Tuesday, 6 August 2013

EF DbContext: inserted record to table (without SaveChanges()), why can't get it in query?

EF DbContext: inserted record to table (without SaveChanges()), why can't
get it in query?

I have code like this
var context = new MyDbContext(); // db context generated from
database
var id = Guid.NewGuid();
var cust = new customer()
{ Id = id, Name = "John" };
context.Customers.Add(cust);
// context.SaveChanges();
var res = context.Customers.Where(c => c.Id == id);
MessageBox.Show(res.Count().ToString());
I inserted a record to a table and when I am running a query I am
expecting that result will contain this new record. But in fact it
doesn't. It works only if I make SaveChanges() before.
What am I doing wrong, why it doesn't work that way?

No comments:

Post a Comment