I'm trying to return several lists of elementes through a WCF Service and I want to control every object loaded in the list to avoid stack overflow exceptions on serializing and returning the result.
I populate this lists with LINQ to Entities and I have disabled Lazy Loading for the EF model... but when I try it many of the references are populated and it throws an exception when trying to return the List.
This is an example:
public class DelegacionesDAO : IDelegacionesDAO
{
Model.Entities entities = new Model.Entities();
public DelegacionesDAO()
{
entities.ContextOptions.ProxyCreationEnabled = false;
entities.ContextOptions.LazyLoadingEnabled = false;
}
public List<Model.Things> GetDelegaciones()
{
IQueryable<Model.Things> thingsList= from things in entities.Things
select things;
return thingsList.ToList<Model.Things>();
}
}
This table "Things" is referenced by other like "Persons" (who have things) and this is the problem, when I try to return it, every "Thing" has a list of "Persons" and beyond.
Am I missing anything??
Thanks in advance!
Edit: Added some more code. Now it's working... I think it loads every navigation property when you try to "watch" it even if it's not loaded :-S
Now I only have the doubt if I'm doing it well by creating the entities instance in the constructor to use it through WCF.
Don't work with long-living contexts (field "entities in your case) when working with WCF:
If you have Lazy Loading turned off you have to tell EF to load the references.
Try this:
var thingsList= (from things in entities.Things
select things) as ObjectQuery<Model.Things>;
thingsList = thingsList.Include("Persons"); //name of navigation property
return thingsList.ToList<Model.Things>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.