Pomelo.EntityFrameworkCore.MySql: Table '__efmigrationshistory' doesn't exist
I’m trying to use ASP.NET Core Identity with MySQL, but when updating the database using the tools I’m getting an error saying the table __efmigrationshistory
doesn’t exist.
If I don’t use the migrations, but only the ctx.Database.EnsureCreated();
it creates the database without any problem.
Steps to reproduce
Sample code:
namespace TestEFCoreMySql
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
public static class Program
{
public static IConfigurationRoot ConfigurationRoot { get; }
static Program()
{
var cfgBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true);
ConfigurationRoot = cfgBuilder.Build();
}
public static void Main(string[] args)
{
Console.WriteLine("Application started...");
try
{
MainAsync(args, CancellationToken.None).Wait();
}
catch (Exception e)
{
Console.WriteLine("An unhandled exception has occured");
Console.WriteLine(e);
}
finally
{
Console.WriteLine("Application terminated. Press <enter> to exit...");
Console.ReadLine();
}
}
public static async Task MainAsync(string[] args, CancellationToken ct)
{
var testSecurityConnection =
ConfigurationRoot.GetConnectionString("TestSecurityConnection");
var optionsBuilder = new DbContextOptionsBuilder<TestSecurityContext>()
.UseMySql(testSecurityConnection);
using (var ctx = new TestSecurityContext(optionsBuilder.Options))
{
// this works, but no ´__efmigrationshistory´ table is created
ctx.Database.EnsureCreated();
}
}
}
public class TestSecurityUser : IdentityUser
{
}
public class TestSecurityContext : IdentityDbContext<TestSecurityUser>
{
public TestSecurityContext(DbContextOptions options) : base(options)
{
}
}
public class TestSecurityContextFactory : IDbContextFactory<TestSecurityContext>
{
public TestSecurityContext Create(DbContextFactoryOptions options)
{
var testSecurityConnection =
Program.ConfigurationRoot.GetConnectionString("TestSecurityConnection");
var optionsBuilder = new DbContextOptionsBuilder<TestSecurityContext>()
.UseMySql(testSecurityConnection);
return new TestSecurityContext(optionsBuilder.Options);
}
}
}
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true,
"copyToOutput": {
"include": [
"appsettings*.json"
]
}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
},
"dependencies": {
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": {
"type": "build",
"version": "1.0.0-preview2-final"
},
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Pomelo.EntityFrameworkCore.MySql": "1.0.0"
},
"frameworks": {
"net4.6.2": {}
}
}
The issue
When running the tools to migrate I get the missing table error
dotnet ef migrations add Initial
dotnet ef database update
Pomelo.Data.MySql.MySqlException (0x80004005): Table 'test_security.__efmigrationshistory' doesn't exist
at Pomelo.Data.MySql.MySqlStream.ReadPacket()
at Pomelo.Data.MySql.NativeDriver.GetResult(Int64& affectedRow, Int64& insertedId)
at Pomelo.Data.MySql.Driver.NextResult(Int32 statementId, Boolean force)
at Pomelo.Data.MySql.MySqlDataReader.NextResult()
at Pomelo.Data.MySql.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection)
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
Table 'test_security.__efmigrationshistory' doesn't exist
Further technical details
MySQL version: 5.7.15 Operating system: Windows 10 Pomelo.EntityFrameworkCore.MySql version: 1.0.0
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 26 (12 by maintainers)
I’m pretty sure this happens when you have created the database (e.g.
CREATE DATABSE db_name
) already. If that’s the case, you need to manually add the__EFMigrationsHistory
table.@Kagamine maybe we should add a check for the
__EFMigrationsHistory
table upon running migrations and add if it does not exist?