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

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

问题描述

在我的项目中,我想在使用EntityFramework的DAL层上运行一些单元测试。在每次运行测试之前,我正在创建一个新的数据库(为了在进行测试时始终具有相同的初始数据)。在测试结束时,这个数据库被删除(所有这些都是在[ClassInitialize()]和[ClassCleanup()]属性的帮助下自动进行的。



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



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



非常感谢任何帮助。



谢谢。

解决方案

当您创建ObjectContext时,您将需要使用构造函数重载一个ConnectionString作为参数。



您可以使用 EntityConnectionStringBuilder 。更具体地说,假设您的基础数据库是SQL Server,您可以使用 SqlConnectionStringBuilder 来构建EntityConnectionStringBuilder.ProviderConnectionString的值。



这里是一些构建SQL Server连接字符串的代码

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

这里有一些构建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;


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;

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

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