I have an application using Entity Framework 6.
I have project named Data where I have my DbContext
.
public partial class MyDbContext: DbContext
{
static MyDbContext()
{
Database.SetInitializer<MyDbContext>(null);
}
public MyDbContext()
: base("Name=" + Utility.Constants.DbConnectionName)
{
Database.SetInitializer<MyDbContext>(null);
}
}
Where Utility.Constants.DbConnectionName = "TestConnection"
In my web.config of course I have:
<add name="TestConnection" connectionString="Data Source=MYSERVERIPADDRESS;Initial Catalog=unitdb;User ID=sa;Password=12345;Connect Timeout=600;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Then I also have a project that is a Windows Service.
Inside that project I have a reference to my Data project so I have a access to my Entity Framework entities. Also in my Windows service project I have an App.config
with the same connection string (above) because If not then it won't know where to get the data.
Is there I way I can set inside my Windows service project a connection string dynamically? Because my windows service may connect to a different database (of course that database has the same structure so EF can get the info).