I ran into an annoying issue recently. I’m going to simplify my datamodel here, but the principle is just the same. I have a class “User”. In that class I have a property that is a list of objects the user owns. I also have this class “object”. Because every “object” has an owner, it has a property of type “User”, which links to its owner. Now, what I’m trying to do is basically this
return Json(myUser,JsonRequestBehavior.AllowGet);
When I load the page, it takes like 30 seconds and then I get the error “RecursionLimit exceeded”.
I guess this is because the objects are linking to each other. Now my question is, how can I tell “Json” that it shouldn’t go deeper then 1 level of objects to avoid this?
myUseris probably a type generated by EntityFramework.When you return Json, the framework is going to prepare each property in essence firing off SQL command to lazy-load all the data.
Instead, you should prepare a ViewModel class with concrete properties not attached to EntityFramework and prepare that object as deep as you want it to go.