Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core - Table '*.__EFMigrationsHistory' doesn't exist

I want to stress out that this is .NET Core and the threads about EF 6.0 does not apply to this problem

I created my DbContext and added it in DI, however when I do dotnet ef database update -v it does not want to create the migrations table __EFMigrationsHistory.

Is there some other command that I should do first or this is a bug of EF Core MySQL adapter?

MainDbContext

using Microsoft.EntityFrameworkCore; using MySQL.Data.EntityFrameworkCore.Extensions; using Web.Models;  namespace Web.Infrastructure {     public class MainDbContext : DbContext     {         public DbSet<User> Users { get; set; }          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)         {             optionsBuilder.UseMySQL("connection-string-here");             base.OnConfiguring(optionsBuilder);         }     } } 

Error

Finding DbContext classes...

Using context 'MainDbContext'.

Using database 'db' on server 'localhost'.

MySql.Data.MySqlClient.MySqlException: Table 'db.__EFMigrationsHistory' doesn't exist

Table 'db.__EFMigrationsHistory' doesn't exist ```

project.json

{   "dependencies": {     "Microsoft.NETCore.App": {       "version": "1.0.1",       "type": "platform"     },     "Microsoft.AspNetCore.Diagnostics": "1.0.0",     "Microsoft.AspNetCore.Mvc": "1.0.1",     "Microsoft.AspNetCore.Razor.Tools": {       "version": "1.0.0-preview2-final",       "type": "build"     },     "Microsoft.AspNetCore.Routing": "1.0.1",     "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",     "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",     "Microsoft.AspNetCore.StaticFiles": "1.0.0",     "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",     "Microsoft.Extensions.Configuration.Json": "1.0.0",     "Microsoft.Extensions.Logging": "1.0.0",     "Microsoft.Extensions.Logging.Console": "1.0.0",     "Microsoft.Extensions.Logging.Debug": "1.0.0",     "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",     "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",     "BundlerMinifier.Core": "2.2.301",     "WebMarkupMin.AspNetCore1": "2.2.1",     "MySql.Data.EntityFrameworkCore": "7.0.6-IR31",     "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"   },   "tools": {     "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",     "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",     "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"   },   "frameworks": {     "netcoreapp1.0": {       "imports": [         "dotnet5.6",         "portable-net45+win8"       ]     }   },   "buildOptions": {     "emitEntryPoint": true,     "preserveCompilationContext": true   },   "runtimeOptions": {     "configProperties": {       "System.GC.Server": true     }   },   "publishOptions": {     "include": [       "wwwroot",       "**/*.cshtml",       "appsettings.json",       "web.config"     ]   },   "scripts": {     "prepublish": [ "bower install", "dotnet bundle" ],     "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]   } } 

Temp Solution

By executing dotnet ef migrations script I get SQL code that I can execute directly in MySQL. After that migrations table is created and everything works normally. This is temp-solution which is bad. I still wonder what is the "correct" way of enabling migrations.

like image 275
Stan Avatar asked Nov 14 '16 20:11

Stan


People also ask

What is custom migration history table in EF Core?

Custom Migrations History Table. By default, EF Core keeps track of which migrations have been applied to the database by recording them in a table named __EFMigrationsHistory. For various reasons, you may want to customize this table to better suit your needs.

How does EF Core keep track of migrations?

By default, EF Core keeps track of which migrations have been applied to the database by recording them in a table named __EFMigrationsHistory. For various reasons, you may want to customize this table to better suit your needs.

How to track which migrations have been applied to the database?

Thank you. By default, EF Core keeps track of which migrations have been applied to the database by recording them in a table named __EFMigrationsHistory. For various reasons, you may want to customize this table to better suit your needs.

Why can't EF create a table in PostgreSQL?

This functionality is implemented by the database provider, not EF itself. There was at least one case where the Npgsql provider for PostgresSQL didn't create the table. There was another similar question in 2016. This is a bug of the official MySQL provider. The fix is to create the table. Not the only one either.


1 Answers

Turning Mark G's comment into an answer.

Once the __EFMigrationsHistory table has been created, the rest of the update should run.

CREATE TABLE `__EFMigrationsHistory` ( `MigrationId` nvarchar(150) NOT NULL, `ProductVersion` nvarchar(32) NOT NULL, PRIMARY KEY (`MigrationId`) ); 

Alternatively, generate the script of your migration(s) and apply to the database manually using this command in Package Manager Console:

Script-Migration 

If you need to generate All scripts, you can use this command:

Script-Migration -from 0 
like image 69
Derrick Avatar answered Sep 20 '22 11:09

Derrick