Johan
Johan

Reputation: 35194

seralizing EF entities; disabling Proxy Creation and Lazy Loading?

Ive been having some problems regarding circular references when converting EF entities to json objects. I have found a solution that works for me, but i would like to know what is actually happening when im adding

 context.ContextOptions.ProxyCreationEnabled = false;

and/or

context.ContextOptions.LazyLoadingEnabled = false;

The first one seems to be enough to get a single-dimensional json object. Should i use both? And what does they actually do? Is there a smarter way around this? Thanks

Upvotes: 5

Views: 3879

Answers (1)

Piotr Perak
Piotr Perak

Reputation: 11088

First line turns off runtime generation of class that inherits from your entity class. This class is really used during runtime. Not your class. This runtime generated class is probably not Serializable and that's why this line (turning off proxy generation) makes serialization work.

Second line turns off lazy loading. So let's say you have Parent entity and Child entity. When you ask for Parent you don't load Children when lazy loading is on. When it is off whenever you load Parent you load all of it's children. I think this is something you should familiarize with if you are using EF.

Edit: If there is problem with circular reference then you should turn off lazy loading. Then when you serialize Parent you will not try to serialize Children that have reference to Parent (creating circular reference)

Upvotes: 6

Related Questions