Entity framework code first migration to multiple database

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

Question

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.

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);
    migrator.Update();
    migration_config.TargetDatabase = new DbConnectionInfo("BlogContextCopy");
    migrator = new DbMigrator(migration_config);
    migrator.Update();

Connection strings for example in app_config file:

<connectionStrings>
 <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;"/>
</connectionStrings>

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; }
}

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);
    migrator.Update();
  }
}

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);
MigrateDatabases(databases);

Works like a charm for me :)




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why