Entity framework adds initial migration creates the database but not the __MigrationHistory table

asp.net-core entity-framework-core


In a new code-first asp.net-core project connecting to my SQL-server database. When I run

add-migration MyInitialMigration

a migration file is created, then executed in SQL server creating the database, table structure with fields, indexes, etc. and populating the tables with the seed data from my DBInitialiser. The table __MigrationHistory is not created in the database and the ModelSnapshot.cs file is created. At this point I haven't run


which from everything I can find, I should need to run before the database, seed data, etc. is actually created. As far as I can tell, I've not got automatic migrations enabled.

Because of this, subsequent migrations aren't applied to the database because the tables already exist, which I'm guessing it figures out by looking for the __MigrationHistory table and not finding any rows in it.

How can I either get the initial add-migration to add the __MigrationHistory table or how do I get the migration to only happen when I enter update-database?

Not sure if it's related, but another project that was migrating fine last week now doesn't work properly. The migration .cs file is created fine, but when it automatically tries to create the database, etc. (when previously I needed to run update-database ) I get the error

Invalid JSON primitive:  "C:\\x\\Migrations\\20171108125832_MyInitialMigration.cs",
  "metadataFile": "C:\\x\\Migrations\\20171108125832_MyInitialMigration.Designer.cs",
  "snapshotFile": "C:\\x\\Migrations\\MyContextModelSnapshot.cs"

which relates to the following command I the migrations .cs file migrationBuilder.CreateIndex( name: "IX_tablea_tableID2", table: "tablea", column: "tableID2"); which as far as I can tell is identical to the several blocks of code above it which all work fine. I've tried deleting the migration files and database and rerunning the add-migration with the same effect. Each time I delete everything and start again like this, a different index causes the error message, even if it worked the previous time.

Has there been an update which breaks/alters how migrations work that I've missed?

11/8/2017 1:09:20 PM

Popular Answer

You must have something odd in your code, like calling dbContext.Database.EnsureCreatedAsync(). This will create tables and database, but no migrations.

In previous versions of ASP.NET Core / EF Core (1.x), the Configure method of Startup class would not be executed, when running the EF Core tools (either add-migration in Powershell or dotnet ef migrations add in command line).

In order for the __MigrationHistory Table to be created, the migration must be applied with either dbContext.Database.MigrateAsync() or by database-update/dotnet ef database update.

However, with the new versions the migrations (or the dbContext.Database.EnsureCreated()) will be applied on every EF Core tools command.

The new pattern for performing DbContext discovery is to have a BuildWebHost static file, which configures the whole application and have extension methods in Main(...) method to perform the migration and seeding.

My previous answer on the new way to apply migrations (and seeding) covers the code.

The ASP.NET Core Annoucement documents this changes (its always a good idea to subscribe to this GitHub repository to receive notifications about new features and breaking changes)

11/8/2017 5:36:04 PM

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