entity framework - EF6: Why Does Count return 0 but Find return an element? -
in 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 guid generated in person ctor // not db-generated identity. console.writeline( "found: {0}", fb != null ); db.savechanges(); console.writeline( "count: {0}", db.people.count() ); } the output looks this:
count: 0 found: true count: 1 i have seen other posts count not being updated until savechanges called. ok, "way works".
my question this: why find return object db.people when count() returns 0 , enumerator returns no elements? shouldn't find , count() act similarly, waiting savechanges before returning entities in "added" state?
what reasoning this? ask because writing light-weight non-sql provider needs mirror actions of ef possible. can't figure out logic causes find return added element count() , getenumerator() doesn't. need address these situations.
find() has special quality: first dbcontext's entities have been newly added not yet flushed database (e.g.: entities entitystate.added).
count() , enumerator instead @ database, doesn't have yet until call savechanges().
Comments
Post a Comment