Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 313
Johan Avatar asked Nov 02 '11 21:11

Johan


1 Answers

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)

like image 99
Piotr Perak Avatar answered Sep 27 '22 19:09

Piotr Perak