c# - Simple DbSet<TEntity>.Find() Call Taking Forever -


i getting terrible performance dbset.find() call. code looks this:

public class area {     [key]     public string id { get; protected set; }      // ... }  public class mycontext : dbcontext {     //...     public dbset<area> areas { get; set; }     //... }  // call takes long area area = context.areas.find(id);     

i understand has search through entity set, check change tracking, etc. , trigger call database. problem it's taking orders of maginitude longer simple context.areas.singleordefault(x => x.id == id) call. more think reasonable. following tip on question, tried temporarily turning off change tracking without success (it doesn't appear have had effect):

try {     context.configuration.autodetectchangesenabled = false;     return context.areas.find(id); } {     context.configuration.autodetectchangesenabled = true; } 

to try , bottom of this, fired profiler. found:

trace

it looks it's taking time preparing execution plan. why take long during .find() call , not explicit .singleordefault call (notice near top of call stack, preparing singleordefault call). there way see query .find() method trying compile?

i never did figure out why taking long. query generated looked reasonable (just simple select). i'm using pretty complicated domain model, , while area entity described above isolated , simple, perhaps entity framework somehow trying build views or generate query touches other parts of domain model (or rather, trying decide shouldn't).

in case, did formulate work around. trick manually work (i thought) find() call doing in first place:

public area findarea(string id) {   // first see if dbset has in local cache   area area = context.areas.local.singleordefault(x => x.id == id);    // query database   return area ?? context.areas.singleordefault(x => x.id == id); } 

this technique generalized using extension method. particular implementation assumes entity stores it's id in string id column , implements following interface. however, tweaking adapt kinds of domain models.

public interface ientitywithid {   string id { get; } }  public static object fastfind<tentity>(this dbset<tentity> dbset, string id)   tentity : ientitywithid, class {   // first see if dbset has in local cache   tentity entity = dbset.local.singleordefault(x => x.id == id);    // query database   return entity ?? dbset.singleordefault(x => x.id == id); }       

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -