Search code examples
mysqlentity-frameworkef-code-first.net-coreentity-framework-core

Initial Add-Migration thinks database is already up to date


I am trying to do a complete reset of EF Core code-first migrations in my project. I have a good reason to do so (can explain if needed), but am having a hard time getting it to work.

I've dropped the previous database and deleted the old migrations directory in my project.

See below log. About three quarters of the way down, you see that EF thinks the database is already up to date. However, you see that it created a fresh database at the beginning? Note: I truncated a lot at the end of the log as it is just stack trace errors because it couldn't find the table that it thought already should have existed.

Any guidance on how to re-create a fresh initial migration and update the database?

PM> Add-Migration InitialCreate
Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\CollinB\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.0.1-rtm-125 initialized 'FilterListsDbContext' using provider 'Pomelo.EntityFrameworkCore.MySql' with options: MaxPoolSize=128 MigrationsAssembly=FilterLists.Api 
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE DATABASE `filterlistsdata`;
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (33ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE `__EFMigrationsHistory` (
          `MigrationId` varchar(95) NOT NULL,
          `ProductVersion` varchar(32) NOT NULL,
          CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
      );
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (18ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='filterlistsdata' AND TABLE_NAME='__EFMigrationsHistory';
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT `MigrationId`, `ProductVersion`
      FROM `__EFMigrationsHistory`
      ORDER BY `MigrationId`;
Microsoft.EntityFrameworkCore.Migrations[20405]
      **No migrations were applied. The database is already up to date.**
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT CASE
          WHEN EXISTS (
              SELECT 1
              FROM `languages` AS `l`)
          THEN TRUE ELSE FALSE
      END
MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'filterlistsdata.languages' doesn't exist ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'filterlistsdata.languages' doesn't exist

Solution

  • I figured it out. I didn't realize that the application actually runs when I run the initial add-migration. It was running some of my seed code which was causing it to fail. Temporarily commenting that out allowed my initial create to run.