内存IDbSet有什么意义? [英] What's the point of in memory IDbSet?

查看:66
本文介绍了内存IDbSet有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到人们测试基于EntityFramework(首先是代码)的解决方案的一种方法是为其自定义上下文创建接口,该接口包含IDbSet类型的属性(而不是DbSet)。然后在单元测试中,他们使用InMemoryDbSets。

I see one of the ways people test solutions based on EntityFramework (code first) is to create interface for their custom context that contains properties of type IDbSet (instead of DbSet). Then in unit tests they use InMemoryDbSets.

我是EntityFramework的新手,这似乎是个不错的方法。但这根本不起作用吗?当我们使用InMemory DbSet时,我们可以创建和运行使用我们实体对象的任何属性的查询。即使这些是计算的属性。但是在运行时,相同的查询将引发 LINQ to Entities不支持指定的类型成员'PropertyName'。仅支持初始化程序,实体成员和实体导航属性。。

I'm new to EntityFramework and that seemed like great way to do it. But it doesn't work at all does it? When we are using InMemory DbSets we can create and run queries that use any of the properties of our entity objects. Even if those are calculated properties. But during runtime the same queries will throw "The specified type member 'PropertyName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.".

因此,如果您使用带有IDbSets的自定义上下文,那么我可能不了解(可能),或者根本无法进行单元测试。

So either I don't understand something (probably) or it is not possible to unit test at all if you are using custom context with IDbSets.

推荐答案

好吧,我做了很多研究,似乎唯一合理的方法(如果要公开IDbSet)是进行集成测试。只需忘了对其进行单元测试。

Ok I did a lot of research and seems that the only reasonable way (if You want to expose IDbSet) is to do integration testing. Just forget about unit testing it.

Ladislav Mrnka在这里很好地总结了它。
实体框架4.1的虚假DbContext测试

Ladislav Mrnka summarized it here very nicely. Fake DbContext of Entity Framework 4.1 to Test

我也在Ayende的博客上看到( http ://ayende.com/ ),这就是他的做法。

Also I saw on Ayende's blog (http://ayende.com/) that this is the way he is doing it.

这篇关于内存IDbSet有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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