I've experimented with these methods of entity retrieval:
Generic, with Single
: ~0.05s
var obj = MyDbContext.Set<Person>().Single(p => p.Id == 2);
Generic, with Find
: ~4s
var obj = MyDbContext.Set<Person>().Find(2);
Non-generic, with Find
: ~4s
var obj = (Person)MyDbContext.Set(typeof(Person)).Find(2);
And I can't figure out, why time is so differ?
I've read this old question: DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID
And it is not my issue, because I have
MyDbContext.Configuration.AutoDetectChangesEnabled = false;
While it is the first query on MyDbContext
instance, I'm doing a lot of work with another instances before these measurments and caching can be related somehow.
Second and next calls by all three methods are fast.
The problem is that I can't use generic method, because I receive the type of set at run-time.