如何判断上下文中的任何实体是否与.Net Entity Framework 4.0相同 [英] How to tell if any entities in context are dirty with .Net Entity Framework 4.0

查看:92
本文介绍了如何判断上下文中的任何实体是否与.Net Entity Framework 4.0相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在实体框架上是否有未保存的数据。我已经弄清楚如何使用ObjectStateManager来检查现有实体的状态,但是我有两个问题。


  1. 我希望单个函数调用以查看任何实体是否未保存,而不是环境中的所有实体循环。

  2. 我无法确定如何检测我添加的实体。这表明我不完全了解实体上下文的工作原理。例如,如果我有ObjectSet myContext.Employees,并且我添加一个新的员工到这个集合(用.AddObject),我看不到新的实体,当我看着ObjectSet,我也看不到.Count增加。但是,当我做一个上下文.saveChanges(),我的新实体被持久化...嗯?

我一直无法在我的msdn搜索中找到答案,所以我希望有人能够告诉我。



提前感谢。

解决方案

  var addedStateEntries = Context 
.ObjectStateManager
.GetObjectStateEntries(EntityState.Added);


I want to be able to tell if there is any unsaved data in an entity framework context. I have figured out how to use the ObjectStateManager to check the states of existing entities, but there are two issues I have with this.

  1. I would prefer a single function to call to see if any entities are unsaved instead of looping though all entities in the context.
  2. I can't figure out how to detect entities I have added. This suggests to me that I do not fully understand how the entity context works. For example, if I have the ObjectSet myContext.Employees, and I add a new employee to this set (with .AddObject), I do not see the new entity when I look at the ObjectSet and I also don't see the .Count increase. However, when I do a context.SaveChanges(), my new entity is persisted...huh?

I have been unable to find an answer to this in my msdn searches, so I was hoping someone here would be able to clue me in.

Thanks in advance.

解决方案

var addedStateEntries = Context
    .ObjectStateManager
    .GetObjectStateEntries(EntityState.Added);

这篇关于如何判断上下文中的任何实体是否与.Net Entity Framework 4.0相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆