2

It's possible to partially disable LazyLoading ? For example :

If we have : ParentObject - ChildLevel1- ChildLevel2

I want to disable LazyLoading only for ChildLevel1 , so when the ParentObject are loaded , the ChildLevel1 should not be loaded automatically. But when I manually will load ChildLevel1 , for childlevel2 the Lazyloading should function as usual.

It's this possible ?

Thank you !

1 Answer 1

1

Entity Framework requires your navigation properties to be marked as public virtual and not sealed to enable lazy loading. So, to achieve your escenario, just remove the virtual keyword from your ChildLevel1 navegation property in the ParentObject class.

I suggest you read this post. Another useful link describing this is MSDN's Requirements for Creating POCO Proxies

Sign up to request clarification or add additional context in comments.

2 Comments

But the problem is that I don't want this permanently. Only in some cases I want this scenario , in other cases I need LazyLoading to work as usual for ChildLevel1 too.
Sorry, but I'm afraid that is not possible.If you define your navigation property virtual, EF will at runtime create a new class (dynamic proxy) derived from your ParentObject class and use it instead. This new dynamically created class contains logic to load navigation property when accessed for the first time. This is not something that you can enable or disable at runtime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.