Reputation:
I use entity framework 6 code first.
At the moment I have a database for production. I want to add a different database for tests. How do I do that?
Do I need another connection string in my tests project?
What should be the difference between the two connection strings?
I tried copying the connection string and change the Catalog name but it results in mapping to the same database.
Please help me understand each part of the connection string in general
Thanks
Upvotes: 3
Views: 5612
Reputation: 5623
Using local db in the local test environment
You can use localdb for your testing environment. Localdb is a file based database on your local development system which does not need a server to run but behaves like an SQL Server from your applications point of view.
I can describe the steps I would perform in Visual Studio 2013:
mdf
file) in my users folder e.g. C:\Users\USERNAME\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0
Data Source=(localdb)\v11.0;Initial Catalog=DATABASENAME;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
Use this Connection string to create a new connectrion string in your web.config or app.config within the connectionStrings
element. Put the value in the connectionString
attribute. Do not change the providerName
attribute.
<connectionStrings>
<add name="ConnectionName" connectionString="..." providerName="System.Data.SqlClient" />
</connectionStrings>
Use that connection string name in your DbContext derived class:
public class YourDbContext : DbContext
{
public YourDbContext() : base("ConnectionName")
{
}
}
Upvotes: 8