保存更改之前的EntityFramework显示实体 [英] EntityFramework show entities before saving changes
问题描述
实体框架对象集,其方法ToList显示刚才保存的实体。这意味着,当我打电话
Entity Framework ObjectSet with its method ToList shows just saved entities. That means, when I call
context.AddToCustomers(myNewCust);
和则(不调用调用SaveChanges)
and then (without calling SaveChanges)
myDataGrid.DataContext = context.Customers.ToList();
在DataGrid不显示新添加的实体(甚至 context.Customers .Count之间的()
不包括它)。
有什么办法来显示这些实体(那些 EntityState =新增
)?
Is there any way to show these entities (those with EntityState = Added
) ?
感谢。
推荐答案
我觉得你可以通过调用像得到保存的补充实体:
I think you can get unsaved added entities by calling something like:
var inserted = context.ObjectStateManager
.GetObjectStateEntries(EntityState.Added)
.Where(e => !e.IsRelationship)
.Select(e => e.Entity)
.OfType<Cutomer>();
但是,仅仅通过阅读你的问题,我affraid你正在尝试做的事情是错误的。为什么你需要保存的实体结合了检索?如果您需要显示未保存的内容,您应该简单地把它放在自己的单独收集。
But just by reading your question, I'm affraid that you are trying to do something wrong. Why do you need to combine unsaved entities with retrieved? If you need to show unsaved content you should simply keep it in your own separate collection.
这篇关于保存更改之前的EntityFramework显示实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!