Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Multiple databases with one context

I have a context in my database that points to 3 child database instances. I inject the correct db connection string via the context's construtor.

However I'm having difficulty getting this to work with automatic migrations. The issue is, automatic migrations expects a a parameterless constructor which I can't provide and IDbContextFactory only allows me to return one connection string.

Is there a way I can get the migration scripts to run against multiple databases or would I need to create 3 separate contexts?

like image 425
heymega Avatar asked Jul 20 '15 11:07

heymega


People also ask

Can we use multiple database in Entity Framework?

Entity Framework : A Comprehensive Course Multiple DbContext was first introduced in Entity Framework 6.0. Multiple context classes may belong to a single database or two different databases.

How can I use multiple databases in a single ASP NET project?

Solution 1 & use it while login user in. 2) Store user specific connection string in user login details table in another column or in another table. 3) During user log in get user data with specific connection string & store it in session. 4) Further use the connection string from session & do your actions.

What is services AddDbContext?

AddDbContext<TContext>(IServiceCollection, ServiceLifetime) Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET.

What is context in EF?

The context class is a most important class while working with EF 6 or EF Core. It represent a session with the underlying database using which you can perform CRUD (Create, Read, Update, Delete) operations. The context class in Entity Framework is a class which derives from System.


1 Answers

Each instance of your context has one database connection.

Assuming that each child database wille have the same code-first model, you can launch one instance of the same context class for each database.

Just call DbContext.Initialize( true ) to migrate the database, then close the connection.

var context1 = new MigratorContext( connectionString1 );
context1.Initilialize( true );
var context2 = new MigratorContext( connectionString2 );
context2.Initilialize( true );
var context3 = new MigratorContext( connectionString3 );
context3.Initilialize( true );

Add a constructor for MigratorContext taking the connection string:

public MigratorContext(string connString)
   : base( connString)
{
    var migrationConfiguration = new MigrationConf();

    Database.SetInitializer<MigratorContext>(
        new MigrateDatabaseToLatestVersion<
            MigratorContext, MigrationConf>(true, migrationConfiguration));
}

public sealed class MigrationConf : DbMigrationsConfiguration<MigratorContext>
{
    public MigrationConf()
        : base()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }
}
like image 84
Anthony Brenelière Avatar answered Oct 23 '22 15:10

Anthony Brenelière