13

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?

6
  • @Fabio The __EFMigrationsHistory table isn't an entity of mine it's implicitly created by EF to manage which migrations to apply. Commented May 25, 2018 at 10:38
  • just make sure you use unique migration names when generating the migrations, I think the problem must be 2 dbcontexts with migrations named alike Commented May 25, 2018 at 11:25
  • Which version of EF Core are you using? Which Provider? As stated here multiple dbcontext on same database (and even same schema) shouldn't cause any issues Commented May 25, 2018 at 11:25
  • also make sure they have unique table names Commented May 25, 2018 at 11:26
  • @JoeAudette The migration names are absolutely unique between contexts. Commented May 25, 2018 at 11:29

1 Answer 1

21

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

Sign up to request clarification or add additional context in comments.

6 Comments

Multiple Contexts using the same database shouldn't be an issue as pointed by @bricelam in this comment. So you are maybe doing something "funky" and the two contexts share the same migration IDs?
I mean just separate migration history for each context it will be fixed, because two context try to create same migration history table name and the two table with same name same schema are not allowed
I have multiple dbcontexts from different assemblies working fine in my projects and they all use the same migration history table, seems weird to have to do that.
I agree with @JoeAudette, something strange seems about that setup. While it max fix the immediate problem, it won't fix the root of problem of it.
@ChrisPickford for example see the method EnsureDataStorageIsReady in my Program.cs here it calls multiple static methods in different assemblies and each one does migrations like this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.