Initial Add-Migration thinks database is already up to date

.net-core ef-code-first entity-framework entity-framework-core mysql

Question

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
1
0
2/6/2018 3:48:45 PM

Accepted Answer

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.

1
1/18/2018 6:14:24 PM

Popular Answer

Never manually delete a Migrations folder. It will save you a lot of headache; you use code-first to create your database so use code-first to delete.

First, close visual studio and re-open it. Then try to run your migration again.

if that doesn't work, The easiest thing to do at this point, would be to go to appsettings.json and change the name of your database. Then run add-migration again and update the database.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow