Entity framework 无法在集成测试中切换连接
Entity framework can't switch connection in Integration test
我有一个正常的项目,在 app.config 中很好地配置了连接。我有一个测试项目,其中的连接也配置得很好。这一切都很完美。
但是当我想在代码中切换连接时,似乎没有任何效果。它似乎完全忽略了我在构造函数中传递的连接字符串,并且它一直使用默认连接(由与数据库上下文的类名匹配的配置指定。
我的测试项目配置:
<!-- Connection string -->
<connectionStrings>
<add name="MyApplication.Database.MyContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test1.mdf" providerName="System.Data.SqlClient" />
<add name="TestEntities" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test2.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
还有我的(仍然)小测试:
using (var db = new MyContext(@"name=TestEntities"))
{
db.Database.Initialize(true);
}
这样做是在 C:\DB\Test1.mdf 而不是 C:\DB\Test2.mdf 创建一个新数据库。这很奇怪,因为如果我删除连接字符串,我会收到无法找到它的错误。如果我调试测试,我什至可以在连接对象中看到正确的连接字符串。
我不知道我做错了什么。
有效的方法(但有点 weird/ugly)是将我的 DatabaseContext 子类化,以便它获得一个新名称。
P.S。这是我的整个 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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<!-- Connection string -->
<connectionStrings>
<add name="MyApplication.Database.MyContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test1.mdf" providerName="System.Data.SqlClient" />
<add name="TestEntities" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test2.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
没有更多代码,我只能猜测,但我怀疑您错过了对基类构造函数的调用。它应该是这样的:
public class MyContext : DbContext
{
public MyContext(string connectionString) : base(connectionString) { }
}
在你整个app.config。 TestEntities 连接字符串不存在。检查配置中的“+ TestEntities”
编辑:
此外,如果上下文 class 名称有一个与配置中定义的名称相同的连接字符串,这将优先。手动连接字符串定义将被忽略。
请在此 link 中搜索 'using the existing connectionstring' : https://books.google.co.za/books?id=X63NBgAAQBAJ&pg=PA157&lpg=PA157&dq=entity+framework+connection+string+precedence&source=bl&ots=ZjPMWaidLV&sig=zmo40T5myWllmNzlVI8rS25-tXo&hl=en&sa=X&ei=RkCQVfjuJ-OP7AbuoKu4CQ&ved=0CCsQ6AEwBQ#v=onepage&q=entity%20framework%20connection%20string%20precedence&f=false
我有一个正常的项目,在 app.config 中很好地配置了连接。我有一个测试项目,其中的连接也配置得很好。这一切都很完美。
但是当我想在代码中切换连接时,似乎没有任何效果。它似乎完全忽略了我在构造函数中传递的连接字符串,并且它一直使用默认连接(由与数据库上下文的类名匹配的配置指定。
我的测试项目配置:
<!-- Connection string -->
<connectionStrings>
<add name="MyApplication.Database.MyContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test1.mdf" providerName="System.Data.SqlClient" />
<add name="TestEntities" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test2.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
还有我的(仍然)小测试:
using (var db = new MyContext(@"name=TestEntities"))
{
db.Database.Initialize(true);
}
这样做是在 C:\DB\Test1.mdf 而不是 C:\DB\Test2.mdf 创建一个新数据库。这很奇怪,因为如果我删除连接字符串,我会收到无法找到它的错误。如果我调试测试,我什至可以在连接对象中看到正确的连接字符串。
我不知道我做错了什么。
有效的方法(但有点 weird/ugly)是将我的 DatabaseContext 子类化,以便它获得一个新名称。
P.S。这是我的整个 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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<!-- Connection string -->
<connectionStrings>
<add name="MyApplication.Database.MyContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test1.mdf" providerName="System.Data.SqlClient" />
<add name="TestEntities" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=C:\DB\Test2.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
没有更多代码,我只能猜测,但我怀疑您错过了对基类构造函数的调用。它应该是这样的:
public class MyContext : DbContext
{
public MyContext(string connectionString) : base(connectionString) { }
}
在你整个app.config。 TestEntities 连接字符串不存在。检查配置中的“+ TestEntities”
编辑:
此外,如果上下文 class 名称有一个与配置中定义的名称相同的连接字符串,这将优先。手动连接字符串定义将被忽略。
请在此 link 中搜索 'using the existing connectionstring' : https://books.google.co.za/books?id=X63NBgAAQBAJ&pg=PA157&lpg=PA157&dq=entity+framework+connection+string+precedence&source=bl&ots=ZjPMWaidLV&sig=zmo40T5myWllmNzlVI8rS25-tXo&hl=en&sa=X&ei=RkCQVfjuJ-OP7AbuoKu4CQ&ved=0CCsQ6AEwBQ#v=onepage&q=entity%20framework%20connection%20string%20precedence&f=false