更改数据库名称在连接字符串在运行时的实体框架 [英] change db name in connection string at runtime in Entity Framework

查看:133
本文介绍了更改数据库名称在连接字符串在运行时的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我想运行正在使用的EntityFramework在DAL层的一些单元测试。我是从scrips创建测试的每次运行前一个新的数据库(以便具有始终相同的初始数据执行测试时)。在试验结束时,该数据库被丢弃,(全部是与[ClassInitialize()]和帮助自动进行[ClassCleanup()]属性

生成的数据库总是有不同的名称,像TestDB的-2009-01-31--12-00-00,为了不与我collegues的测试数据库冲突。

这是我的实际问题是,我没有找到一个还没有办法告诉的EntityFramework连接到生成的数据库(名字是在运行时产生)。现在它连接到app.config文件中指定的连接字符串,这当然是正常的。而且因为我在做这些测试,我在找的东西,可以从外部的DAL的DLL来完成(不设置任何直接的EF上下文)。

任何帮助是极大的AP preciated。

感谢。

解决方案

在创建ObjectContext中,您将需要使用构造函数重载需要的ConnectionString作为参数。<​​/ P>

您可以通过建立这个ConnectionString中的<一个href="http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.aspx">EntityConnectionStringBuilder.更具体地说,假设你的基础数据库为SQL Server,你可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx">SqlConnectionStringBuilder建立起来的价值EntityConnectionStringBuilder.ProviderConnectionString。

下面是一些code,它建立了SQL Server连接字符串

  VAR SCSB =新SqlConnectionStringBuilder();
scsb.DataSource =localhost的;
scsb.InitialCatalog =的MyDB;
scsb.IntegratedSecurity = TRUE;
 

和这里的一些code,它建立实体的ConnectionString:

  VAR建设者=新EntityConnectionStringBuilder();
builder.Metadata =RES://*/MyModel.csdl |高分辨率://*/MyModel.ssd​​l |高分辨率://*/MyModel.msl;
builder.Provider =System.Data.SqlClient的;
builder.ProviderConnectionString = scsb.ConnectionString;
 

In my project I want to run some unit tests on the DAL layer that is using EntityFramework. I'm creating from scrips a new database before each run of the tests (in order to have always the same initial data when doing the tests). At the end of the tests, this database is dropped, (all is made automatically with the help of [ClassInitialize()] and [ClassCleanup()] attributes.

The generated database always has a different name, something like TestDB-2009-01-31--12-00-00, in order not to conflict with the test databases of my collegues.

The actual problem that I have is that I did not find yet a way to tell EntityFramework to connect to the generated database (the name is generated at runtime). Right now it connects to the connection string specified in the app.config file, which is normal, of course. And because I'm doing these tests, I'm looking for something that can be done from outside the DAL dll (without setting anything on the EF context directly).

Any help is greatly appreciated.

Thanks.

解决方案

When you create the ObjectContext, you will need to use the constructor overload that takes a ConnectionString as a parameter.

You can build this ConnectionString using an EntityConnectionStringBuilder. More specifically, assuming your underlying database is SQL Server, you can use a SqlConnectionStringBuilder to build up the value for EntityConnectionStringBuilder.ProviderConnectionString.

Here is some code that builds up the SQL Server connection string

var scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "localhost";
scsb.InitialCatalog = "MyDB";
scsb.IntegratedSecurity = true;

And here's some code that builds the Entity ConnectionString:

var builder = new EntityConnectionStringBuilder();
builder.Metadata = "res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl";
builder.Provider = "System.Data.SqlClient";
builder.ProviderConnectionString = scsb.ConnectionString;

这篇关于更改数据库名称在连接字符串在运行时的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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