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.
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.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With