我有这样的代码
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());
我在表中插入了一条记录,当我运行查询时,我期望该结果将包含此新记录。但实际上并非如此。仅当我之前执行SaveChanges()时它才有效。
我在做什么错,为什么它不能这样工作?
最佳答案
尝试查询本地对象
http://msdn.microsoft.com/en-us/library/gg696248(v=vs.103).aspx
context.Customers.Local.yourqueryhere
这将查询上下文跟踪的实体,包括尚未保存的实体。
关于c# - EF DbContext:将记录插入表中(没有SaveChanges()),为什么无法在查询中获取它?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18089787/