Entity framework code first migration to multiple database

code-first ef-code-first ef-migrations entity-framework entity-framework-6


Lets say we have the architecture model of web application where we have 1 database per 1 account. Database structure is the same for these accounts and differs only on data with in. How can i configurate a migrations in code first model.

1/6/2014 8:44:13 AM

Accepted Answer

Now I have next solution. In the main method or in global.asax something like this:

    var migration_config = new Configuration();
    migration_config.TargetDatabase = new DbConnectionInfo("BlogContext");
    var migrator = new DbMigrator(migration_config);
    migration_config.TargetDatabase = new DbConnectionInfo("BlogContextCopy");
    migrator = new DbMigrator(migration_config);

Connection strings for example in app_config file:

 <add name="BlogContext" providerName="System.Data.SqlClient" connectionString="Server=(localdb)\v11.0;Database=MigrationsDemo.BlogContext;Integrated Security=True;"/>
 <add name="BlogContextCopy" providerName="System.Data.SqlClient" connectionString="Server=(localdb)\v11.0;Database=MigrationsDemo.BlogContextCopy;Integrated Security=True;"/>

Configuration class and context:

internal sealed class Configuration : DbMigrationsConfiguration<MigrationsDemo.BlogContext>
    public Configuration()
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;

    protected override void Seed(MigrationsDemo.BlogContext context) {

public class BlogContext : DbContext {
    public BlogContext() {}
    public BlogContext(string connection_name) : base(connection_name) {
    public DbSet<Blog> Blogs { get; set; }
1/7/2014 6:27:16 AM

Popular Answer

In addition to your excellent answer, you can use an external config file (i.e. "clients.json") instead of hardcoding them, put all the database infos in key-value pairs into the json file and load it during startup.

Then, by iterating over the key-value pairs, you can do the initialization.

The clients.json:

  "DatabaseA": "DatabaseAConnectionString",
  "DatabaseB": "DatabaseBConnectionString",
  "DatabaseC": "DatabaseCConnectionString",

Provide a method to handle the migrations:

public static void MigrateDatabases(IDictionary<string,string> databaseConfigs)
  foreach (var db in databaseConfigs)
    var config = new Configuration
      TargetDatabase = new DbConnectionInfo(db.Value, "System.Data.SqlClient")

    var migrator = new DbMigrator(config);

Then during startup, (I use OWIN, so it's in my Startup.cs, could also be global.asax.cs):

string json;
var path = HttpRuntime.AppDomainAppPath;
using (var reader = new StreamReader(path + @"Config\clients.json"))
   json = reader.ReadToEnd();

var databases = JsonConvert.DeserializeObject<IDictionary<string, string>>(json);

Works like a charm for me :)

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow