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

3
  • possible duplicate of EF6 DBContext Dynamic Connection String Commented Oct 31, 2014 at 22:18
  • create a constructor that takes a configuration name? Commented Oct 31, 2014 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") ? Commented Oct 31, 2014 at 22:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.