I'm using EF 4.1 and code-first in an MVC project, and AutoMapper to map entities to view models.
Prior to using code-first I was able to exclude navigation properties in order to prevent anything from being loaded that wasn't already. I'm using .Include() in my queries to include the references that I need in order to avoid additional database round-trips.
However, with code-first my entity only exposes an entity property (or ICollection if there are more than one). How can I know whether it has been loaded without triggering the load?
Assuming this can be done, is there a way to make this the default behavior for AutoMapper, so that I do not have to explicitly exclude members on every single entity?
We can disable lazy loading for a particular entity or a context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false.
Lazy loading is the practice of delaying load or initialization of resources or objects until they're actually needed to improve performance and save system resources.
Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. Lazy loading means delaying the loading of related data, until you specifically request for it.
One form of lazy loading is infinity scroll, in which, the content of the web page is loaded as and when the user scrolls down the page. It is a popular technique being used by various websites. Advantages of Lazy loading: On-demand loading reduces time consumption and memory usage thereby optimizing content delivery.
You can check whether a reference or collection navigation property of an entity
has been loaded by:
bool isLoaded1 = dbContext.Entry(entity).Reference(e => e.MyReferenceProperty)
.IsLoaded();
bool isLoaded2 = dbContext.Entry(entity).Collection(e => e.MyCollectionProperty)
.IsLoaded();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With