Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What effect does the virtual keyword have on entity properties in Entity Framework 5?

I know that navigation properties in Entity Framework must have the virtual keyword in front of them or else EF can't do it's navigation property magic. But what effect does putting the virtual keyword in front of other properties have? I saw an example recently wherein every single property on the entity was declared with the virtual keyword. Does EF 5 really lazy load every property if I do that or am I missing something about how that works?

like image 669
Chev Avatar asked Nov 04 '22 14:11

Chev


1 Answers

Using virtual keyword for navigation properties is necessary for lazy loading. Using virtual keyword on all mapped properties is necessary for dynamic change tracking. All requirements both options are described in documentation.

In EF 4.0 dynamic change tracking was recommended approach and because of that POCO T4 Generator used virtual properties but since EF 4.1 snapshot change tracking is recommended as initial option with shift to dynamic change tracking only when shapshot change tracking is victim of performance issues.

like image 101
Ladislav Mrnka Avatar answered Nov 15 '22 06:11

Ladislav Mrnka