0

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).

halfer
  • 19,824
  • 17
  • 99
  • 186
VAAA
  • 14,531
  • 28
  • 130
  • 253
  • possible duplicate of [EF6 DBContext Dynamic Connection String](http://stackoverflow.com/questions/21165364/ef6-dbcontext-dynamic-connection-string) – MikeH Oct 31 '14 at 22:18
  • create a constructor that takes a configuration name? – Marvin Smit Oct 31 '14 at 22:19
  • So instead a Name=something I can set the connection string in the constructor like: MyDBContext("Data Source=MYSERVERIPADDRESS;Initial Catalog=unitdb;User ID=sa;Password=12345;Connect Timeout=600;MultipleActiveResultSets=True" providerName="System.Data.SqlClient") ? – VAAA Oct 31 '14 at 22:31

0 Answers0