Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Is it possible to load in a same context from differents connection string?

I have 4 identical databases. Identical in structure but each of them contains specific data. I have on context.

C#
Namespace.Data.dbContext dbContext_1 = new Namespace.Data.dbContext();
Namespace.Data.dbContext dbContext_2 = new Namespace.Data.dbContext();
Namespace.Data.dbContext dbContext_3 = new Namespace.Data.dbContext();
Namespace.Data.dbContext dbContext_4 = new Namespace.Data.dbContext();


but in my config file I have this

HTML
<add name="dbContext_1" connectionString="..." providerName="System.Data.EntityClient"/> 
<add name="dbContext_2" connectionString="..." providerName="System.Data.EntityClient"/> 
<add name="dbContext_3" connectionString="..." providerName="System.Data.EntityClient"/> 
<add name="dbContext_4" connectionString="..." providerName="System.Data.EntityClient"/> 

And I get an exception because Entity Framework tell me it doesn't find the connection string for dbContext.


It seems EF load by default a connection string with his class name. Correct? How can I make it load other connection string?

FYI I use EntityFramework 6 and I use T4 templates to generate my DbContext.

What I have tried:

I already tried to load my other connection strings by doing

Namespace.Data.dbContext dbContext_1 = new Namespace.Data.dbContext("dbContext_1");
Namespace.Data.dbContext dbContext_2 = new Namespace.Data.dbContext("dbContext_2");
Namespace.Data.dbContext dbContext_3 = new Namespace.Data.dbContext("dbContext_3");
Namespace.Data.dbContext dbContext_4 = new Namespace.Data.dbContext("dbContext_4");

Or

Namespace.Data.dbContext dbContext_1 = Namespace.Data.dbContext.???;
Namespace.Data.dbContext dbContext_2 = Namespace.Data.dbContext.???;
Namespace.Data.dbContext dbContext_3 = Namespace.Data.dbContext.???;
Namespace.Data.dbContext dbContext_4 = Namespace.Data.dbContext.???;
Posted
Updated 14-Apr-16 1:38am

1 solution

So you can target a connection string with a DbContext, but you need to make sure that your class implementation has a constructor that accepts a string parameter and passes it to the base constructor. You will also need to access the connection strings manually. Your derived DbContext will need to look something like this:

C#
public class MyDbContext : DbContext
{
   ... // DbSets and whatnot

   public MyDbContext(string connStr) : base(connStr)
   {
      // Any other needed constructor logic
   }

}


Now you can spin off a little factory class that will return a copy of your DbContext with the appropriate connection target. There's no resilience in this sample, and if you want to have a default return context you can replace the exception with a default return.

C#
public class DbSelector
{
   public static MyDbContext GetContext(string name)
   {
      var connStrConfig = ConfigurationManager.ConnectionStrings[name];

      if(connStrConfig != null 
         && !string.IsNullOrWhitespace(connStrConfig.ConnectionString))
      {
         return new MyDbContext(connStrConfig.ConnectionString);
      }else{
         throw new Exception("Invalid Configuration String requested.");
      }
   }
}


From there, getting a targeted MyDbContext is simple.

C#
DbSelector.GetContext("dbContext_1");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900