6
votes

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?

2

2 Answers

7
votes

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.

0
votes

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.