I think I can give the same answer that I gave here
To save an object, you usually add it to it with a DbSet in context.
for instance
var bar = new Bar(); bar.Name = "foo"; var context = new Context(); context.Bars.Add(bar);
Surprisingly, the context.Bars request cannot be found, only the added object
var howMany = context.Bars.Count(b => b.Name == "foo");
After context.SaveChanges() 1 will appear 1 the same line
DbSet seems unaware of the changes until they are saved to db.
Fortunately, each DbSet has a Local property that acts like the DbSet itself, but reflects all operations in memory
var howMany = context.Bars.Local.Count(b => b.Name == "foo");
You can also use Local to add entities.
context.Bars.Local.Add(bar);
and get rid of the strange behavior of the Entity Framework.
source share