Understanding code first virtual properties

后端 未结 3 1795
再見小時候
再見小時候 2020-12-03 02:55

Hi I am just learning to work with Entity Framework Code First and I can not seem to understand something.I have created three models based on a tutorial:

pu         


        
相关标签:
3条回答
  • 2020-12-03 03:23

    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.

    0 讨论(0)
  • 2020-12-03 03:28

    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

    0 讨论(0)
  • 2020-12-03 03:32

    Virtual properties are there to allow lazy loading

    0 讨论(0)
提交回复
热议问题