c# - EF loading virtual navigation property without accessing -
i have following models:
public class category { public virtual icollection<product> products{get;set;} public product() { products = new hashset<product>(); } } public class product { public guid categoryid{get;set;} public virtual category {get;set;} }
now if execute following statement:
var list = await uow.categories.query.where(x=>x.name.contains("mob")).tolistasync();
and return list
json
mvc controller action. throws following exception:
a circular reference detected while serializing object of type 'system.data.entity.dynamicproxies.category_7c2191cfexxxxxxx'.
it happening because products
collection not null , each product
in turn contains category
.
what reason virtual properties getting uploaded automatically?
edit:- turns out json serializer accessing properties , causing ef load them. have turned lazyloading
off suggested haim770.
the reason entity framework proxy intercepting access call products
property , automatically populates it.
you can explicitly turn lazy-loading off though:
context.configuration.lazyloadingenabled = false;
you can project category
list new list , populate desired properties. example:
var list = await uow.categories.query.where(x=>x.name.contains("mob")) .select(x => new category { id = x.id, name = x.name }).tolistasync();
Comments
Post a Comment