.Net EF Core Flashcards
How to migrate database changes from EF core to database?
Using database migrations , by running commands or by calling migrate method in startup of the application
What happens when you write filtering , sorting , paging before execute command?
The query runs inside DB instead of in code which improves the performance of query
What are the two types of queries ?
- Normal, read-write query
2.AsNoTracking, read-only query
What are different types of loading data?
Eager loading, Explicit loading, Select loading, Lazy loading
Will EFCore load relationships by default?
No , it will not . By default , they will be null
What is Eager loading?
Eager loading is when you query one type of entity and immediately load related entities as part of it. Eager loading is specified via two fluent methods , Include and thenInclude
What is the issue with eager loading?
If you do not specify .Include() , no related entities are loaded or only partial set may be loaded if previously some related entities are loaded
What is the advantage of eager loading?
Loads all the data of the related entity using minimum database round trips
What is the disadvantage of eager loading?
Since it loads all the data of the related entity , in some cases you might do not want to load all of the columns
What are the linq methods you can add in your Include() methods?
Where, OrderBy, OrderByDescending, ThenBy, ThenByDescending, Skip and Take
What is explicit loading?
After loading primary entity class , you can explicitly load any other relationships using Load() method
What is the issue with explicit loading?
A seperate db query is run each time you call load() . So if you were to load table of N records then N+1 queries need to be processed instead of just one query in case of eager loading
What is the disadvantage of explicit loading?
More database round trips compared to eager loading. If you know upfront that you need related entities use eager loading as it takes fewer round trips . On the other hand you know you need to load related entities for fewer instances use explicit loading
What is select loading?
Use the select linq method to select few standard properties from entity and its related entity.
What is advantage of select loading?
Only the required properties are loaded
What is the disadvantage of select loading?
Need to explicity specify the needed properties
What is Lazy Loading?
With lazy loading , navigational properties are loaded automatically only when needed . It has the same cost that of explict loading
How to setup lazy loading?
It can be setup by adding proxies library when configuring db context - locks you into setting up lazy loading for all relationships . Second option is by injecting a lazy loading method into the entity class via its constructor by DI
How to configure simple loading through dbcontext?
Add keyword virtual before all properties and add method UseLazyLoadingProxies when setting up uour dbcontext
How lazy loading works?
Since you have configured lazy laoding in entity classes and in the way you create the dbcontext, reading relationships is simple. You don’t need extra Include method in your query because data is loaded from db when your code access that relationship property
What is client vs server evaluation feature?
This feature gives you the oppartunity to adapt/change the data within the last part of the query, which can save you from having to apply an extra step after the query
Can you use any LINQ command on client vs server evaluation property?
No , an InvalidOperationException is returned with message - could not be translated
What is Iqueryable<T> type?</T>
If you want use sort , filtering and paging on a Type , it should implement IQueryable
What is State property ?
Every entity class instance has State property which tells EF core what to do with this instance when savechanges is called . Possible values for state are Added, Unchanged, Modified, Deleted, Detached