37) What is Connected Scenario in Entity Framework?
Connected Scenario is when the Entity is retrieved from the database and some operations like modification is done in the same context.
- Disable the Change Tracking if it’s not required.
- Use the compiled query whenever required.
- Avoid using Views
- Fetch the required data from database.
40) What are the ways we can load the related entities in Entity Framework?
Below are the ways to load the entities in Entity Framework –
- Lazy Loading
- Eager Loading
- Explicit Loading
41) What is Lazy Loading in Entity Framework?
Lazy Loading is the default behavior of Entity Framework, wherein the dependent/related entities are loaded once they are accessed for the first time.
42) Give an example for Lazy Loading?
Consider a scenario of Student and Courses. Student can enroll to multiple Courses.
Filter the Student based on Student Id.
Student s = dbContext.Students.FirstOrDefault(a => a. StudentId == sId);
Load all the Courses related to Student.
List<Course> courses = s.Courses;
43) What is Eager Loading in Entity Framework?
Eager Loading will load the dependent/related entities at once. Unlike Lazy loading, Eager loading will do only one database call and get all the dependent entities.
44) Give an example for Eager Loading?
Consider a same Student scenario -
Student s = dbContext. Students.Include(s => s. Courses).FirstOrDefault(s => s. StudentId == sId);
45) What is Explicit Loading in Entity Framework?
Be default Entity Framework supports Lazy loading, but we can disable the Lazy loading and we can still load the dependent/related entities by calling “Load” method.
46) Give an example for Explicit Loading?
Continuing Student Scenario -
Student s = dbContext. Students.FirstOrDefault(a => a. StudentId == sId);
dbContext.Entry(s).Reference(s => s. Courses).Load();
47) Which type of loading is good in which scenario?
- Use Eager Loading, when the relations are not too much so that we can get the data in one database query.
- Use Eager Loading, if you are sure that we are going to use the dependent/related entities.
- If there are one-to-many relations use Lazy loading and if the dependent/related entities are not required down the line.
- When Lazy Loading is turned off, use Explicit loading when you are not sure whether dependent/related entities are not required down the line.
48) Can we use Stored Procedures in Entity Framework?
49) Do Entity Framework handle the Change Tracking?
50) How Entity Framework handles the Change Tracking?
Entity Framework supports automatic change tracking of all the loaded entities through context class.
51) What is the minimum requirement for Change Tracking in Entity Framework?
If Entity Framework need to handle Change Tracking, each entity should have Entity Key (Primary Key).