在单元测试中无法创建我的DbContext [英] Can not create my DbContext in a Unit test

查看:132
本文介绍了在单元测试中无法创建我的DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中创建TLPContext时,我在本页底部收到异常。

When I create my TLPContext in a unit test I get an exception at the bottom of this page.

我在单元测试中做到了这一点:

I did this in the unit test:

DbContext context = new TLPContext();
context.Database.CreateIfNotExists();

这不正确吗?

我的连接字符串/提供程序有什么问题?

What is wrong with my connection string / provider ?

我已经遵循: http://www.thereforesystems.com/turn-on-msdtc-windows-7/

但在重新启动后没有帮助。

but it did not help after a reboot.

我的单元测试项目中的我的app.config文件:

Thats my app.config file in my unit test project:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <connectionStrings>
    <add name="TLPContext" providerName="System.Data.SqlClient" connectionString="Data Source=LISA\SQLEXPRESS;Initial Catalog=TLPTEST;Integrated Security=True;Pooling=False;"/>
  </connectionStrings>
</configuration>

这是我的DbContext,它应该足以创建数据库但不是表...

That is my DbContext, it should be enough to create the db but not the tables...

public class TLPContext : DbContext
{

}

这是我得到的例外:

当我创建一个TLPContext实例我的单元测试SetUp:

when I create a TLPContext instance in my unit test SetUp:

Test 'TLP.DataAccess.UnitTests.GenericRepositoryTests.Test' failed: System.Data.ProviderIncompatibleException : An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
---- System.Data.ProviderIncompatibleException : Der Anbieter hat keine ProviderManifestToken-Zeichenfolge zurückgegeben.
-------- System.Data.SqlClient.SqlException : MSDTC on server 'LISA\SQLEXPRESS' is unavailable.
    at System.Data.Entity.ModelConfiguration.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
    at System.Data.Entity.ModelConfiguration.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)


推荐答案

如果不使用连接字符串,它会自动找到数据库服务器(它搜索SQLEXPRESS),并根据项目的名称创建数据库。

If you wouldn't use a connection string, it would automatically find the database server (it searches for SQLEXPRESS) and create the database based on your project's name.

您缺少的是给出连接字符串或其名称,可以通过使用DbContext的构造函数之一来实现:

What you are missing is giving the connection string or its name, which can be achieved by using one of the DbContext's constructor:

TLPContext(string connectionStringOrName) : DbContext(connectionStringOrName) {}

希望这有帮助!

这篇关于在单元测试中无法创建我的DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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