EntityFramework 在保存更改之前显示实体
实体框架 ObjectSet 及其方法 ToList 显示刚刚保存的实体。这意味着,当我调用
context.AddToCustomers(myNewCust);
然后(不调用 SaveChanges)
myDataGrid.DataContext = context.Customers.ToList();
时,DataGrid 不会显示新添加的实体(甚至 context.Customers.Count()
也不包含它)。
有什么方法可以显示这些实体(带有 EntityState = Updated
的实体)?
提前致谢。
Entity Framework ObjectSet with its method ToList shows just saved entities. That means, when I call
context.AddToCustomers(myNewCust);
and then (without calling SaveChanges)
myDataGrid.DataContext = context.Customers.ToList();
the DataGrid doesn't show the newly added entity (even context.Customers.Count()
doesn't include it).
Is there any way to show these entities (those with EntityState = Added
) ?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以通过调用类似以下内容来获取未保存的添加实体:
但仅通过阅读您的问题,我担心您正在尝试做错事。为什么需要将未保存的实体与检索到的实体合并?如果您需要显示未保存的内容,您只需将其保存在您自己的单独集合中即可。
I think you can get unsaved added entities by calling something like:
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.
查看 TryGetObjectStateEntry(EntityKey, ObjectStateEntry) 方法
http:// /msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx
Look at the TryGetObjectStateEntry(EntityKey, ObjectStateEntry) method
http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx