0

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

update-database

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?

  • You must have something odd in your code, like calling `dbContext.Database.EnsureCreatedAsync()`. This will create tables and database, but no migrations. Migrations need to be applied with `dbContext.Database.MigrateAsync()`. Also do not seed/migrate within Startup.cs anymore. The new patter in ASP.NET Core 2.0/EF Core 2.0 requires to use a static `BuildWebHost` method, like in [this answer](https://stackoverflow.com/questions/45941707/why-remove-migration-run-my-app/45942026#45942026) – Tseng Nov 08 '17 at 15:23
  • That's the way I'm doing the seeding at the moment. I'd had `context.Database.EnsureCreated();` in the initialise function from the beginning a month ago. I've just commented it out and now everything is working fine just like it was last week. Commenting it out worked for both problems. Post your comment as an answer and I'll mark it as right. –  Nov 08 '17 at 15:57

1 Answers1

0

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)

Tseng
  • 61,549
  • 15
  • 193
  • 205