在 EF4 中模拟实体上下文 [英] Mocking Entity Context in EF4

查看:28
本文介绍了在 EF4 中模拟实体上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VS2010 B2 和 EF4 B2 并尝试使用 Rhino Mocks 来模拟 EEF 生成的实体上下文.

I am using VS2010 B2 and EF4 B2 and trying to use Rhino Mocks to mock the entity context generated by EEF.

var context = MockRepository.GenerateMock<SomeDBEntities>();
IObjectSet<TxMode> objectSet = new List<TxMode> { mode }.AsObjectSet();
context.Expect(c => c.TxModes).Return(objectSet);

问题是 c.TxModesObjectSet 类型的属性,而我试图返回 IObjectSet> 我得到以下编译错误:

The problem is that c.TxModes is a property of type ObjectSet<TxMode> whereas I am trying to return an IObjectSet<TxMode> and I get the following compile error:

Error 4 The best overloaded method match for 

'Rhino.Mocks.Interfaces.IMethodOptions<System.Data.Objects.ObjectSet
<Data.Entities.TxMode>>.Return(System.Data.Objects.ObjectSet
<Data.Entities.TxMode>)' has some invalid arguments.

AsObjectSet 扩展方法来自这里.

The AsObjectSet extension method comes from here.

知道如何编译它吗?

推荐答案

你不能这样做.查看 Julie Lerman 的系列帖子

You cannot do it this. Check out the serie of post from Julie Lerman

  1. 敏捷实体框架 4 存储库:第 1 部分 - 模型和 POCO 类
  2. 敏捷实体框架 4 存储库:第 2 部分 - 存储库
  3. 敏捷 EF4存储库:第 3 部分 - 微调存储库
  4. 敏捷 EF4 存储库第 4 部分:编译的 LINQ 查询
  5. 敏捷实体框架 4存储库第 5 部分:IObjectSet 和包含

她很好地解释了如何使用 EF4 构建存储库模式以让您模拟它.还有 T4 模板,但这会让您了解所有这些是如何工作的.如果没有经过改编的 T4 模板,要让它运行起来仍然需要做很多工作,但我觉得这是一个很好的练习.:)事实上,我现在正在自己实施它.

She explains very well how to build the repository pattern with EF4 to let you mock that. There are also T4 Template, but that will let you understand how all that works. Without the adapted T4 Template, it is still quite a lot of work to have it up and running, but I find it a very good exercise. :) I'm in fact implementing it myself right now.

祝你好运;)

这篇关于在 EF4 中模拟实体上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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