2013-02-20 35 views
0

当我在单元测试中创建我的TLPContext时,在本页底部出现异常。无法在单元测试中创建我的DbContext

我这样做的单元测试:

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

这是不正确的做法?

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

我都遵循这样的:http://www.thereforesystems.com/turn-on-msdtc-windows-7/

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

这就是我的app.config文件在我的单元测试项目:

<?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,它应该足以创建数据库而不是表...

public class TLPContext : DbContext 
{ 

} 

这是我得到的例外:

当我在我的单元测试中创建一个TLPContext实例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) 

回答

0

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

你所缺少的是给连接字符串或它的名字,它可以使用的DbContext的构造之一来实现:

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

我希望这有助于!

+0

argh ...在TLPContext():base(“DefaultConnection”)之前,这也是app.config中连接字符串的name属性的值。嗯......它现在的工作很奇怪,也许我不再使用context.Database.CreateIfNotExists方法,因为数据库实际上是在第一次调用SaveChanges时创建的。无论如何它现在的作品,谢谢! – Elisabeth 2013-02-20 21:56:21

相关问题