The virtual
keyword just makes it possible that a derived class can be created dynamically at runtime which overrides the properties that are marked as virtual
in order to inject some data access code ("proxy"). But when you set LazyLoadingEnabled = false
you tell Entity Framework "Don't override my entity class with a lazy loading proxy". In that case the virtual
keyword doesn't have any effect. (Well, I can't actually tell what are the costs of instantiating an object that has virtual
properties or methods from .NET CLR viewpoint. It probably has some costs, but I'm sure it's almost nothing compared to actually accessing the database.)
So, your approach to mark properties as virtual
to be open for possible lazy loading in the future is fine in my opinion.
BTW: This - whereas if you leave the virtual keyword out, it will be eager-loaded - is wrong. If you disable lazy loading - either by omitting the virtual
keyword or by setting LazyLoadingEnable = false
- you don't get eager loading by default. Instead you get no loading (of navigation properties) at all. Eager loading must be coded explicitly by using Include
or by using projections.
Just to mention it: You can also add the virtual
modifier later when you need it. And you can limit it to specific entities. EF doesn't consider this as a model change which would touch somehow the database schema.