Reputation: 18485
With EF code first "Association properties that are marked as “virtual” will by default be lazy-loaded".
Is it by convention or there is a logic behind this?
Upvotes: 0
Views: 1185
Reputation: 2295
By marking the property as virtual allows EF to override it in the derived proxy class it wraps around your object. This allows EF to add a loading hook in the overridden property, and enables the lazy loading behaviour.
Microsoft documentation: https://msdn.microsoft.com/en-us/data/jj574232.aspx
The default Entity Framework behaviour is, where possible, to lazy load releated objects. This can be disabled by simply turning off lazy loading for the context, or for individual entities (see link above):
For example, to disable lazy loading for a context:
this.Configuration.LazyLoadingEnabled = false;
Upvotes: 1