Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change db name in connection string at runtime in Entity Framework

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.

like image 920
Lucian Avatar asked Sep 29 '09 16:09

Lucian


People also ask

How do I change the connection string in Entity Framework?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.

How do I change databases in Entity Framework?

When using Entity Framework, the database connection string is stored in the app. config file by default, and the entity object references that to connect to the database. If, for any reason, a user needs to change the database, he can do so simply by editing that file prior to running the program.

How do I change my database connection?

From the Explore Repository, select Tools, and then Change Database Connections. In the Type field, select a report type, then an item, and then click OK. In Change Database Connection, select the item, and then click OK. In Find all references to the Database Connection, select the database connection to change.

What is database connection string name?

Connection String Name property specifies the name of the named connection string that the SQL Workflow Instance Store should use to connect to the persistence database. This parameter is an optional parameter.


1 Answers

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;
like image 130
Mark Seemann Avatar answered Oct 01 '22 14:10

Mark Seemann