2013-02-20 27 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

相關問題