强制更改Flush和查询EF4 Code First CTP实体5 [英] Forcing Change Flushing and Querying of Entities of EF4 Code First CTP 5
本文介绍了强制更改Flush和查询EF4 Code First CTP实体5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以这是我的方案。我正在为我拥有的存储库编写集成测试,并且存储库正在使用EF Code First CTP 5.此集成测试...
- 创建一个对象。 将其传递给一个插入它的save方法。
- 重新获取对象。
- 更改该对象并调用相同的save方法来更新对象。
- 重新获取对象并确认已进行了更改。
这是我的存储库中的save和get方法。 _dataContext类是我的基于DbContext的类。
解决方案
这里有几个选项,你可以强制重新加载:
public Monkey Get(int id)
{
var monkey = _dataContext.Monkeys.Find(id);
_dataContext.Entry(monkey).Reload();
返回猴子;
}
你可以下拉到底层的ObjectContext并为查询使用不同的合并选项:
public Monkey Get(int id)
{
var query =((IObjectContextAdapter)_dataContext).ObjectContext.CreateObjectSet< Monkey>();
query.MergeOption = System.Data.Objects.MergeOption.PreserveChanges;
返回query.FirstOrDefault(m => m.Id == id);
}
~Rowan
So here is my scenario. I am writing an integration test for a repository I have and the repository is using EF Code First CTP 5. This integration test...
- Creates an object. Pass it off to a save method, which inserts it.
- Re-get the object.
- Changes that object and calls the same save method to update the object.
- Re-get the object and confirm that the changes have been made.
Here are my save and get methods on my repository. The _dataContext class is my DbContext-based class.
解决方案
Hi,
There are a couple of options here, you can force a reload:
public Monkey Get(int id) { var monkey = _dataContext.Monkeys.Find(id); _dataContext.Entry(monkey).Reload(); return monkey; }
You can drop down to the underlying ObjectContext and use a different merge option for the query:
public Monkey Get(int id) { var query = ((IObjectContextAdapter)_dataContext).ObjectContext.CreateObjectSet<Monkey>(); query.MergeOption = System.Data.Objects.MergeOption.PreserveChanges; return query.FirstOrDefault(m => m.Id == id); }
~Rowan
这篇关于强制更改Flush和查询EF4 Code First CTP实体5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文