asp.net mvc 3 - Exclude Entity Data from Query -
i'm querying database , have navigation properties don't want return in query. i'm including lot of navigation properties disable lazy loading, run problems producer entity. producer has 1 many relationship wine, , wine has 1 one relationship producer. when run query, want producer information (name, address, phone, etc...), don't need list of wines in associated producer query. there linq method can use include of producer fields? issue because i'm sending object via json, don't want data.
wine w = db.wines.where(n => n.wineid == wineid).include(n => n.vartype).include(n => n.origin).include(n => n.app) .include(n => n.vintage).include(n => n.importer).include(n => n.reviews.select(r => r.publication)) .include(n => n.producer.name).include(n => n.docs).firstordefault(); public class producer : contact { [key] public int producerid { get; set; } public string name { get; set; } public string twitter { get; set; } public virtual icollection<wine> wines { get; set; } public virtual icollection<userobj> userobjs { get; set; } } public class wine :updater { public int wineid { get; set; } //public int winetypeid { get; set; } [display(name = "varietal/type")] public int? vartypeid { get; set; } [display(name = "origin")] public int? originid { get; set; } [display(name = "appellation")] public int? appid { get; set; } [display(name = "vintage")] public int? vintageid { get; set; } [display(name = "importer")] public int? importerid { get; set; } public int producerid { get; set; } public string designate { get; set; } [display(name = "drink window")] public string drinkwindow { get; set; } public string body { get; set; } public string sku { get; set; } [display(name = "case production")] public int? caseproduction { get; set; } [display(name = "alcohol content")] public double? alcoholcontent { get; set; } public string winemaker { get; set; } [display(name = "consulting winemaker")] public string consultwinemaker { get; set; } public bool sustainable { get; set; } public bool kosher { get; set; } public bool organic { get; set; } public bool biodynamic { get; set; } public bool salmonsafe { get; set; } public boolean active { get; set; } public virtual winetype winetype { get; set; } public virtual vartype vartype { get; set; } public virtual origin origin { get; set; } public virtual app app { get; set; } public virtual vintage vintage { get; set; } public virtual importer importer { get; set; } public virtual producer producer { get; set; } public virtual icollection<pos> poss { get; set; } public virtual icollection<review> reviews { get; set; } public virtual icollection<doc> docs { get; set; } public ienumerable<selectlistitem> bodylist { get; set; } }
well, if do
.include(n => n.producer) //not n.producer.name it "eager load" wine's producer, not wine's producer.wines...
another way use anonymous object take desired properties
db.wines.where(n => n.wineid == wineid) .select(w => new { w.vartype.cepage, w.origin.description, ///blabla w.producer.name, w.producer.address.street, }).firstordefault(); edit
look here if still want first solution,. avoid "circular references". entity framework serialize poco json
or solution ef 4.1 - code first - json circular reference serialization error
Comments
Post a Comment