EF Core - Table '*.__EFMigrationsHistory' 不存在

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

我想强调一下,这是.NET Core,关于EF 6.0的线程不适用于这个问题

我创建了我的 DbContext 并将其添加到 DI 中,但是当我这样做时 dotnet ef database update -v 它不想创建迁移 table __EFMigrationsHistory.

我是否应该先执行其他命令,或者这是 EF Core MySQL 适配器的错误?

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);
        }
    }
}

错误

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%" ]
  }
}

临时解决方案

通过执行 dotnet ef migrations script 我得到 SQL 代码,我可以直接在 MySQL 中执行。之后创建了 migrations table 并且一切正常。这是不好的临时解决方案。我仍然想知道启用迁移的 "correct" 方法是什么。

使用标准 Oracle 提供程序时遇到同样的问题。

根据这个问题Dot Net Entity Framework database update doesn't create tables in mysql database它还没有实现迁移功能。

我遵循了切换到 SapientGuardian 提供商的建议,这似乎是现在最好的方式。

编辑:正如评论中所建议的那样,Pomelo 是 2018 年初的最佳选择。自从我最初的回答以来,我已经选择了它而不是其他提供商。

与官方 Oracle MySQL 提供商有同样的问题。

刚刚添加的包:Install-Package SapientGuardian.EntityFrameworkCore.MySql 迁移成功了!

将 Mark G 的评论转化为答案。

创建 __EFMigrationsHistory table 后,其余更新应该 运行。

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

或者,生成迁移脚本并在程序包管理器控制台中使用此命令手动应用到数据库:

Script-Migration

如果需要生成所有脚本,可以使用这个命令:

Script-Migration -from 0

我 运行 遇到了同样的问题,OP 上下文可能略有不同,但为了完整起见,这里是我的回答。

您可以 运行 解决此问题的方法之一是:

  • 您创建迁移并更新数据库,
  • 稍后由于某种原因您删除了表(不是数据库)并再次尝试 运行 update-databse 命令。

在这种情况下,您将收到 OP

报告的错误

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

这种情况下的解决方案是删除整个数据库。之后 update-databse 命令 运行 成功。

我不确定它是否仅与 mysql 有关,但要恢复:

  • 如果您删除表但使用现有数据库(之前有迁移),更新命令会给您一个例外。
  • 如果删除整个数据库,更新命令将运行完美。

我遇到了同样的问题,但环境略有不同。

问题是我尝试 运行 迁移之前数据库已经存在 运行。

简而言之,Make sure that you DO NOT create the database yourself. Let the migrations do it for you.

来自 PHP/Laravel 背景,这对我来说并不明显 :)


MySQL MacOS 上的服务器,如果您对我的环境感兴趣的话。