简体   繁体   中英

Entity Framework Virtual Properties

What is the effect of not marking navigation properties virtual for Entity Framework (v4) entities?

If I don't use lazy loading, but neglect to properly eager load, what will happen upon accessing the (not loaded) property?

Would there be any other impact outside of lazy loading, or is that the main feature that requires virtual navigation properties?

There are two effects. One is on lazy loading, which just won't work if the property is not virtual . But lazy loading isn't always something you want.

The second is change detection. If you're using pure POCOs or are using proxies but don't make the properties virtual , then change detection becomes more involved.

You can learn more about change detection here .

If you are wondering whether Deferred Loading is supported with POCO objects, then I think you will be happy to know that you can get Deferred Loading with POCO as well.

There are two things you need to do in order to get Deferred Loading support with POCO entities:

  1. Declare the property that you would like to load lazily as virtual. These properties can be any collection type that implements ICollection or they can be a reference representing a 1/0..1 relationship.

  2. Enable deferred loading on the context

See MSDN's "POCO in the Entity Framework" post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM