Click here to Skip to main content
15,883,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've heard a lot about lazy loading in MVC (i'm completely new to MVC btw), and alot say you utilise the virutal keyword on properties you wish to lazy load from Entity Framework.

can anyone explain does that mean the virtual keyword has a different meaning in MVC than in standard application where virtual means = 'the property / method can be overriden by the derived class', or does it allow both ?

is this a good way of doing things, and is there any other keyword differences between WebForm usage of c# code and MVC.

sorry i'm just new to MVC and EF, and wanted to clear this up.
Posted

1.Lazy loading related with EF does not depends on MVC. You could find details about it in the next link: http://msdn.microsoft.com/en-us/library/vstudio/dd456846(v=vs.100).aspx[^]
Or if you want info about "Lazy Loading" in general here is a good article: Can you explain Lazy Loading?[^]

2."virtual" is part of C# language and does not depend on the used UI technology: MVC, Web Forms, Windows Forms or WPF.
 
Share this answer
 
By making the properties virtual, entity framework can conjure up dynamic proxies for your objects, and through the overridden properties support lazy loading.

The virtual keyword does not have a different meaning, it just allows the framework to manipulate your object in a way that allows additional functionality (like lazy loading) to work.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Grant Weatherston 21-Nov-14 4:25am    
thanks Frederik, so to clarify would you have to override in order for lazy loading to be feasible, or does EF utilise lazy loading whether you ovveride the property or not?
Fredrik Bornander 21-Nov-14 4:30am    
Pretty much, yeah. EF will create a proxy and it can only override the virtual properties (obviously), and to to lazy loading EF needs to intercept the call to the getter/setter and provide the lazy implementation instead of the standard property implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900