Can not create my DbContext in a Unit test

梦想的初衷 提交于 2020-01-17 08:26:24

问题


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();

Is this not the correct approach?

What is wrong with my connection string / provider ?

I have followed this: http://www.thereforesystems.com/turn-on-msdtc-windows-7/

but it did not help after a reboot.

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>

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

public class TLPContext : DbContext
{

}

That is the exception I get:

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)

回答1:


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.

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) {}

I hope this helps!



来源:https://stackoverflow.com/questions/14989797/can-not-create-my-dbcontext-in-a-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!