Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Migrations with multiple DbContexts on single database

I have an issue attempting to use Migrations in a ASP.NET Core solution using EF Core where there are multiple DbContext that share the same SQL database.

In my application startup method I'm getting a reference to each context and calling the context.Database.Migrate() method. However as both of these contexts are pointing to the same underlying database I'm getting the error:

There is already an object named '__EFMigrationsHistory' in the database.

Here's a MCVE:

class DbContextA : DbContext {}
class DbContextB : DbContext {}

static void Main(string[] args)
{
  var contextA = GetContextFromDIContainer<DbContextA>();
  var contextB = GetContextFromDIContainer<DbContextB>();

  contextA.Database.Migrate();
  contextB.Database.Migrate();
}

void ConfigureServices(IServiceCollection services)
{
  services.AddDbContext<DbContextA>(opt =>
  {
    opt.UseSqlServer("connectionstring");
  });

  services.AddDbContext<DbContextB>(opt =>
  {
    opt.UseSqlServer("connectionstring");
  });
}

Note that each DbContext exists in a separate assembly in which the Migrations are configured.

I am able to manually execute the respective migrations with the Update-Database CLI tool but it doesn't seem to work as part of my app startup code.

Is there a way to execute migrations on both contexts at runtime and bypass the __EFMigrationsHistory table creation if already exists?

like image 891
Chris Pickford Avatar asked May 25 '18 10:05

Chris Pickford


People also ask

How do you add migration to multiple contexts?

Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.

Can we have multiple DbContext in Entity Framework?

In code first, you can have multiple DBContext and just one database. You just have to specify the connection string in the constructor. Yes you can, but how can you query from different entities from different db contexts?

Can we have more than one DbContext?

"More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands."

When should you not use Efcore?

One of the biggest reasons not to use Entity Framework Core is that your application needs the fastest possible data access. Some applications do a lot of heavy data operations with very high-performance demands, but usually business applications don't have that high of a performance demand.


1 Answers

I think your problem is just two Context try to use same migration history table

try specific your migration history table for each

protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(
    connectionString,
    x => x.MigrationsHistoryTable("__MyMigrationsHistoryForDBContextA", "mySchema"));

it should be fix

Custom Migrations History Table

like image 197
Asakuraa Ranger Avatar answered Nov 05 '22 12:11

Asakuraa Ranger