Understanding code first virtual properties

大兔子大兔子 提交于 2019-11-27 13:08:27

It is used to manage lazy loading and change tracking.

EF will generate proxy types on runtime, which are dynamically generated types that inherit from your POCO classes and add all the EF stuff to manage lazy loading / change tracking in the overridden virtual properties.

So virtual is not a "magic keyword" here, virtual is here so your POCOs can be inherited with additional EF-related code at runtime.

Virtual properties are there to allow lazy loading

Freeman

When you create a property or method marked with the virtual keyword, you will be allowed to override it in a derived class, thus offering your method a more specialized behaviour depending on the objects you create.

In the case of Entity Framework its also a convention that points out that lazy loading behaviour is used. A question regarding this matter exists here: Entity Framework 4.1 Virtual Properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!