.Net EF Core Flashcards

1
Q

How to migrate database changes from EF core to database?

A

Using database migrations , by running commands or by calling migrate method in startup of the application

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when you write filtering , sorting , paging before execute command?

A

The query runs inside DB instead of in code which improves the performance of query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two types of queries ?

A
  1. Normal, read-write query
    2.AsNoTracking, read-only query
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are different types of loading data?

A

Eager loading, Explicit loading, Select loading, Lazy loading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Will EFCore load relationships by default?

A

No , it will not . By default , they will be null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Eager loading?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the issue with eager loading?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the advantage of eager loading?

A

Loads all the data of the related entity using minimum database round trips

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the disadvantage of eager loading?

A

Since it loads all the data of the related entity , in some cases you might do not want to load all of the columns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the linq methods you can add in your Include() methods?

A

Where, OrderBy, OrderByDescending, ThenBy, ThenByDescending, Skip and Take

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is explicit loading?

A

After loading primary entity class , you can explicitly load any other relationships using Load() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the issue with explicit loading?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the disadvantage of explicit loading?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is select loading?

A

Use the select linq method to select few standard properties from entity and its related entity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is advantage of select loading?

A

Only the required properties are loaded

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the disadvantage of select loading?

A

Need to explicity specify the needed properties

17
Q

What is Lazy Loading?

A

With lazy loading , navigational properties are loaded automatically only when needed . It has the same cost that of explict loading

18
Q

How to setup lazy loading?

A

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

19
Q

How to configure simple loading through dbcontext?

A

Add keyword virtual before all properties and add method UseLazyLoadingProxies when setting up uour dbcontext

20
Q

How lazy loading works?

A

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

21
Q

What is client vs server evaluation feature?

A

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

22
Q

Can you use any LINQ command on client vs server evaluation property?

A

No , an InvalidOperationException is returned with message - could not be translated

23
Q

What is Iqueryable<T> type?</T>

A

If you want use sort , filtering and paging on a Type , it should implement IQueryable

24
Q

What is State property ?

A

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

25
Q

When does entity instance get tracked?

A

When they are read without using .asnotracking() or after an entity instance has been used as a parameter to EF Core methods(such as Add, Update or Delete), it becomes tracked

26
Q

What happens when the savechanges method is called?

A

EF core runs a method called Detect Changes, which compares the tracking snapshot against the entity class instance that it handed to the application when query was originally executed

27
Q

What is a disconnected update?

A

In this , there are two different instances of application’s DbContext.For example , In web applications ecah HTTP request is typically new request, with no data held feom last HTTP Request

28
Q

What are the two main ways of handling disconnected updates?

A

1.You send only the data uou need to update back from the first stage.
2.you send all the entity data to re-create the entity calss back from first stage

29
Q

What does update command do?

A

It replaces all the row data-updates all columns for the given primary key

30
Q

What is principal entity?

A

Contains a primary key that dependent relationship refer to via a foreign key

31
Q

What is Dependent Entity?

A

Contains a foreign key that refers to principal entity’s primary key

32
Q

Can the dependent entity exist without principal entity?

A

If the primary key of principal entity is nullable in dependent entity . Then even if you delete the principal entity, dependent entity remains unaffected

33
Q

What is connected state update?

A

It assumes that same context is used for both the read and the update