c# - How to serialize entity if using lazy loading in entity framework? -


i started learn entity framework , facing problem related serialization of generated models. have tables 1 many relation country , state 1 country have many states. using db first approach , when create entities using entity framework, class country has 1 property of icollection. read , found navigation property. let me show generated class first below:

//this generated class.

 public class country     {         public country()         {                 states = new hashset<states>();         }         public int id { get; set; }         public string contrycode  { get; set; }          public string contryname  { get; set; }             public virtual icollection<states> states{ get; set; }      } 

i generated models , step forwarded. got problem of serialization when request via ajax list country. googled , found terms lazy loading, eager loading , n+1 problem. read in detail. found solution turning off lazy loading. question how can serialize model lazy loading?

i have created metadata class , use attribute ignorexml etc nothing helped. way using asp.net mvc 5 , want serialize model lazy loading. can 1 explain?

when use lazy loading, execution deferred until value of property needed. encountering error because context has been disposed time property accessed. consider following scenario:

  • you have object called country lazy-loaded property called states.
  • you object context.
  • the context disposed.
  • you call states property.
  • the property goes looking context came from.
  • an error thrown because "the context has been disposed".

code sample:

using(var context = new someentitycontext()) {      var country = context.countries.first(); }  //this throw error because context disposed of. var states = country.states;  

the serializer throw error because go through properties of object, find states , try value.

even if context still alive, run loop navigational properties during serialization. because both objects hold reference each other. consider following scenario:

  • you have object called country lazy-loaded property called states.
  • the serializer attempts serializer object of type country.
  • it reads states collection.
  • it attempts serialize every object of type state.
  • it reads property of type country.
  • it attempts serialize object of type country.
  • it reads states collection.
  • it attempts serialize every object of type state.
  • it reads property of type country.
  • it reads states collection.
  • it attempts serialize every object of type state.
  • it reads property of type country.
  • it reads states collection.
  • it attempts serialize every object of type state.
  • it reads property of type country.
  • .... (endless loop, well, @ least until run out of stack frames).

alternatively, can create custom serializer avoid pitfalls of navigational properties, more work it's worth. approach best suited situations serialized version differs object.

this why better off using data transfer object (dto). map data object , send on wire. there components out there can mapping you, if keep structures similar possible.

check out automapper: http://automapper.org/


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -