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

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

问题描述

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

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

问题是 c.TxModes 类型为 ObjectSet< TxMode> 的属性,而我正在尝试返回一个 IObjectSet< TxMode> ,并获得以下内容编译错误:

 错误4最好的重载方法匹配

'Rhino.Mocks.Interfaces。 IMethodOptions< System.Data.Objects.ObjectSet
< Data.Entities.TxMode>> .Return(System.Data.Objects.ObjectSet
< Data.Entities.TxMode>)'有一些无效参数。

AsObjectSet扩展方法来自这里



任何想法我可以如何编译?

解决方案

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



  1. 敏捷实体框架4存储库:第2-部分存储库

  2. 敏捷EF4存储库:第3部分 - 精细调优存储库

  3. 敏捷EF 4存储库第4部分:编译LINQ查询

  4. 敏捷实体框架4存储库第5部分:IObjectSet和Include

她很好地解释了如何用EF4构建存储库模式,让你嘲笑。
还有T4模板,但这将让您了解所有的工作原理。
没有适应的T4模板,要花费和运行,还有相当多的工作,
,但我觉得这是一个非常好的练习。 :)
我实际上是自己实现的。



祝你好运;)


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);

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.

The AsObjectSet extension method comes from here.

Any idea how I can get this to compile?

解决方案

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

  1. Agile Entity Framework 4 Repository: Part 1- Model and POCO Classes
  2. Agile Entity Framework 4 Repository: Part 2- The Repository
  3. Agile EF4 Repository: Part 3 -Fine Tuning the Repository
  4. Agile EF 4 Repositories Part 4: Compiled LINQ Queries
  5. Agile Entity Framework 4 Repository Part 5: IObjectSet and Include

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.

Good luck ;)

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

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