Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Entitiy Framework cache

Tags:

Is it possible to perform an explicit Where query on the Entity Framework cache? I know that I can use Find to look for an entity in the cache (based on the entities primary key).

Code sample:

var person = new PersonToStoreInDb() { Id = 1, Name = "John" };
dbSet.Add(person);
// Perform some other code
...
// DbContext.SaveChanges was NOT called!
var personFromDbSet = bSet.Where(p => p.Name == "John").First();
// personFromDbSet is null because it was not sent towards DB via SaveChanges
like image 481
Moerwald Avatar asked May 09 '17 11:05

Moerwald


1 Answers

Entity Framework manages the cached data in DbSet.Local. It is an observable collection and Linq queries like Where can be applied to it. It will contain the loaded entries as well as entries of different state like added and removed until SaveChanges is called.

like image 118
grek40 Avatar answered Sep 22 '22 11:09

grek40