asp.net mvc - in Entity framework I want to include only first children objects and not child of child(sub of sub) -


i have these 2 classes:

public class businessestbl {     public string id { get; set; }     public string firstname { get; set; }     public string lastname { get; set; }      public icollection<offerstbl> offerstbls { get; set; } }  public class offerstbl  {     public int id { get; set; }     public string name { get; set; }      public int catid { get; set; }      public string businessestblid { get; set; }     public virtual businessestbl businessestbls { get; set; } } 

when try bring offers according catid field, need return businessestbls also, method return offers again per each businessestbl obj , code :

public iqueryable<offerstbl> getofferstblscat(int id) {     db.offerstbls.include(s => s.businessestbls); } 

you can see wrong result on : http://priooffer.azurewebsites.net/api/offersapi/getofferstblscat/4

as can see return offers under each business object while business object under each offer, , want return offers business object without offer under business obj.

could 1 please?

this happens because entity framework performs relationship fixup, process auto-populates navigation properties when objects belong there present in context. circular references drill down navigation properties endlessly when lazy loading disabled. json serializer (but apparently it's instructed deal circular references, isn't trapped in endless loop).

the trick prevent relationship fixup ever happing. relationship fixup relies on context's changetracker, caches objects track changes , associations. if there's nothing tracked, there's nothing fixup. can stop tracking calling asnotracking():

db.offerstbls.include(s => s.businessestbls)              .asnotracking() 

if besides disable lazy loading on context (by setting contextconfiguration.lazyloadingenabled = false) see offerstbl.businessestbls populated in json string , businessestbl.offerstbls empty arrays.

a bonus asnotracking() increases performance, because change tracker isn't busy tracking objects ef materializes. in fact, should use in disconnected setting.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -