What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?

Up vote 21 down vote favorite 7 share g+ share fb share tw.

The virtual keyword has an effect when used on properties in EF Code First. Can someone describe all its ramifications in different situations? For instance, I know it can control lazy loading -- if you use the virtual keyword on an ICollection/one-to-many relationship property, it will be lazy-loaded by default, whereas if you leave the virtual keyword out, it will be eager-loaded.

What other effects can virtual have in EF with POCO entities? Should I default to using virtual on all my properties, or default to NOT using it? Entity-framework-4 ef-code-first entity-framework-4.1 link|improve this question edited Apr 8 '11 at 16:10 asked Apr 8 '11 at 16:02Scott Stafford5,79711438 68% accept rate.

So far, I know of these effects. Lazy Loading: Any virtual ICollections will be lazy-loaded unless you specifically mark them otherwise. More efficient change tracking.

If you meet all the following requirements then your change tracking can use a more efficient method by hooking your virtual properties. From the link: To get change tracking proxies, the basic rule is that your class must be public, non-abstract or non-sealed. Your class must also implement public virtual getters/setters for all properties that are persisted.

Finally, you must declare collection based relationship navigation properties as ICollection only. They cannot be a concrete implementation or another interface that derives from ICollection (a difference from the Deferred Loading proxy) Another useful link describing this is MSDN's Requirements for Creating POCO Proxies.

6 There is no other reason to make properties virtual. Navigation properties are marked as virtual for lazy loading and scalar properties are marked as virtual for change tracking. – Ladislav Mrnka Apr 8 '11 at 18:31.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions