c# - EF Core - 表 '*.__EFMigrationsHistory' 不存在

标签 c# mysql entity-framework entity-framework-core .net-core

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

我创建了我的 DbContext 并将其添加到 DI 中,但是当我执行 dotnet ef database update -v 时它不想创建迁移表 __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我得到了可以直接在 MySQL 中执行的 SQL 代码。之后创建迁移表,一切正常。这是一个不好的临时解决方案。我仍然想知道启用迁移的“正确”方式是什么。

最佳答案

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

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

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

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

Script-Migration

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

Script-Migration -from 0

关于c# - EF Core - 表 '*.__EFMigrationsHistory' 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40597534/

相关文章:

c# - 如何忽略以0开头的整数

c# - 完成 POST 是否需要 HttpWebRequest.GetResponse?

php - 使用 PDO,是否有更简单的方法来获取给定 id 的单个表变量?

mysql - 如何执行从不同表返回唯一值的 mySQL 查询

c# - 当 DatabaseGenerated.Identity 已经与 Entity Framework 一起使用时自动增加一个数字

javascript - JQuery UI 自动完成未到达 ActionResult C# MVC

mysql - 匹配多表获取角色名

entity-framework - 如何在 Entity Framework Code First 中急切地包含实体的子元素和孙元素?

entity-framework - 无论如何,我是否可以将数据加载到内存中,而不是使用 .csv 文件来进行单元测试?

c# - 未找到具有不变名称 'Oracle.ManagedDataAccess.Client' 的 ADO.NET 提供程序的 Entity Framework 提供程序