In this code
using (var db = new DbPerson())
{
var b = db.People.Create();
b.Name = "Homer";
db.People.Add( b );
Console.WriteLine( "Count: {0}", db.People.Count() );
foreach (var bb in db.People)
Console.WriteLine( bb.Name );
var fb = db.People.Find( b.Id ); // Id is a GUID generated in the Person ctor
// NOT a DB-generated Identity.
Console.WriteLine( "Found: {0}", fb != null );
db.SaveChanges();
Console.WriteLine( "Count: {0}", db.People.Count() );
}
the output looks like this:
Count: 0
Found: True
Count: 1
I have seen other posts about Count not being updated until SaveChanges
is called. OK, so this is the "way it works".
My question is specifically this: Why does Find
return an object from db.People
when Count()
returns 0 and the enumerator returns no elements? Shouldn't Find
and Count()
act similarly, waiting for SaveChanges
before returning Entities that are in the "Added" state?
What is the reasoning for this? I ask because I am writing a light-weight non-SQL provider that needs to mirror the actions of EF as nearly as possible. I can't figure out what logic causes Find
to return an Added element that Count()
and GetEnumerator()
doesn't. I need to address these situations.