Entity Framework is eager loading a single entity -


the abridged version of webapi controller so:

[httpget, route("")] public async task<ihttpactionresult> search(bool includeentities) {     iqueryable<versiontopic> results = dbcontext.versiontopics;     if (includeentities)     {         results = results.include(o => o.createdby);         results = results.include(o => o.lastsavedby);         results = results.include(o => o.topic.lastsavedby);         results = results.include(o => o.topic.createdby);         results = results.include(o => o.topic.packtype.lastsavedby);         // etc...     }      results = results.orderby(o => o.sortorder);      return ok(result.tolist()); } 

for reason, lastsavedby entity populated, when includeentities parameter false.

why eager loading 1 entity none of others (as required)?

here's screenshot:

enter image description here

my model defined so:

public class versiontopic {     [key]     [required]     public guid versiontopicid { get; set; }      [required]     [index("ix_versiontopic_versionid_topicid", isunique = true, order = 0)]     public guid versionid { get; set; }      [required]     [index("ix_versiontopic_versionid_topicid", isunique = true, order = 1)]     public guid topicid { get; set; }      [required(allowemptystrings = true)]     [maxlength(250)]     public string name { get; set; }      public string keymessage { get; set; }      [required]     public int sortorder { get; set; }      [required]     public guid createdbyid { get; set; }      [required]     public datetime createddate { get; set; }      [required]     public guid lastsavedbyid { get; set; }      [required]     public datetime lastsaveddate { get; set; }      public virtual icollection<versionrecommendation> versionrecommendations { get; set; } = new list<versionrecommendation>();      [foreignkey("createdbyid")]     public virtual applicationuser createdby { get; set; }      [foreignkey("lastsavedbyid")]     public virtual applicationuser lastsavedby { get; set; }      [foreignkey("topicid")]     public virtual topic topic { get; set; }      [foreignkey("versionid")]     public virtual version version { get; set; }      public versiontopic()     {         versiontopicid = guid.newguid();     } } 

it's explained in following tip loading related data - eager loading section of ef core documentation, same applies ef6 , below:

entity framework core automatically fix-up navigation properties other entities loaded context instance. if don't explicitly include data navigation property, property may still populated if or of related entities loaded.

unfortunately behavior not controllable, options avoid use fresh new dbcontext (or manually cleaning navigation properties not needed, that's annoying , easy forget).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -