努力单元测试Entity Framework 6.1.3 DB-first [英] Effort unit testing Entity framework 6.1.3 DB-first
问题描述
使用Effort框架(版本1.1.4)对DB层进行单元测试时遇到问题。
I am experiencing problems when using Effort framework (version 1.1.4) to unit test my DB-layer.
我有一个使用Entity Framework 6.1的DB层.3,并且使用数据库优先方法创建模型,因此存在描述该模型的 *。edmx
文件。
I have a DB-layer using Entity framework 6.1.3 and the model is created using database-first approach so there is an *.edmx
file describing the model.
我创建了一个部分类,以公开暴露单元测试使用的其他构造函数,如下所示:
I have created a partial class to expose an additional constructor used by the unit tests with effort like this:
public partial class Entities
{
public Entities(DbConnection connection)
: base(connection, true)
{
}
}
简单的单元测试如下:
private Entities CreateContext()
{
//var connectionString = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
//var connection = Effort.EntityConnectionFactory.CreateTransient(connectionString);
//return new Entities(connection as DbConnection);
var connection = Effort.EntityConnectionFactory.CreatePersistent("name=Entities");
var context = new Entities(connection);
return context;
}
[TestMethod]
public void Testing_Effort_Integration()
{
using (var context = CreateContext())
{
var entity = context.TableEntity.FirstOrDefault(i=> i.Id);
Assert.IsNotNull(entity);
}
}
当我运行单元测试时,它会抛出异常该行:
When I run the unit test it throws an exception for the line:
var connection = Effort.EntityConnectionFactory.CreatePersistent("name=Entities");
{提供者未返回ProviderManifest实例。}
InnerException消息:{无法确定存储版本;需要有效的
存储连接或版本提示。}
{"The provider did not return a ProviderManifest instance."} InnerException Message: {"Could not determine storage version; a valid storage connection or a version hint is required."}
我发现的其他帖子建议从2012年更改 *。edmx
文件中的 ProviderManifestToken
属性改为 2008。这似乎解决了问题,但是当我第一次在这里尝试使用上下文时,却给了我另一个例外:
Other posts I have found suggests to change the ProviderManifestToken
attribute in the *.edmx
file from "2012" to "2008". This seems to solve the problem but instead gives my another exception when trying to use the context for the first time here:
var entity = context.TableEntity.FirstOrDefault(i=> i.Id);
NotSupportedException无法确定
提供者工厂的提供者名称
类型的'System.Data.EntityClient.EntityProviderFactory'。确保
ADO.NET提供程序已安装或在应用程序配置中注册。
NotSupportedException Unable to determine the provider name for provider factory of type 'System.Data.EntityClient.EntityProviderFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
任何人都知道如何解决此问题问题,以便我可以将Effort与实体框架6.1.3结合使用DB-first方法?
Anyone know how to solve this issue so I can use Effort with entity framework 6.1.3 DB-first approach?
我已经成功地能够使用Effort(版本1.1.4)对DB进行单元测试EF 4和EF 5中使用DB-first方法创建的层-这就是为什么我认为EF版本可以引起人们的关注...
I have successfully been able to use Effort (version 1.1.4) to unit test DB-layers created in EF 4 and EF 5 with DB-first approach - this is why I think that the EF-version can be of interest...
推荐答案
我的一位同事找到了解决我问题的方法!
A colleague of mine found the solution to my issue!
显然,我使用的是 Effort nuget软件包,而不是 Effort.EF6 nuget软件包。卸载并安装另一个后,我还必须使用以下标签更新App.Config:
Apparently I was using the "Effort" nuget package instead of the "Effort.EF6" nuget package. After uninstalling and installing the other I also had to update my App.Config with the tags:
<system.data>
<DbProviderFactories>
<add name="Effort.Provider" invariant="Effort.Provider" description="Effort.Provider" type="Effort.Provider.EffortProviderFactory,Effort" />
</DbProviderFactories>
</system.data>
<entityFramework>
<providers>
<provider invariantName="Effort.Provider" type="Effort.Provider.EffortProviderServices, Effort" />
</providers>
</entityFramework>
我还在SetUp中加入了一个电话,供我的单元测试注册努力提供者:
And I also included a call in the SetUp for my unit tests to register the effort provider:
[SetUp]
public void Setup()
{
EffortProviderConfiguration.RegisterProvider();
}
这为我解决了这个问题。希望它可以为其他人提供帮助!
This solved the issue for my. Hopefully it can provide some help to others!
这篇关于努力单元测试Entity Framework 6.1.3 DB-first的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!