Junior — Middle
Can you explain the concept of lazy loading of data in Entity Framework and what are its advantages?
sobes.tech AI
Answer from AI
Lazy Loading in Entity Framework is a mechanism where related data is loaded from the database only when it is first accessed, rather than immediately when the main object is loaded. This helps save resources and reduce the initial query time.
Advantages:
- Reduces the amount of data loaded at once.
- Improves performance when working with large object graphs.
Example:
using(var context = new MyDbContext())
{
var user = context.Users.Find(1); // Only the user is loaded
var orders = user.Orders; // A separate database query is executed here to load the orders
}