mysql - 无法将代码优先迁移应用到 mysql 数据库

标签 mysql asp.net-mvc ef-code-first entity-framework-migrations

我正在使用 EF 代码优先模型开发 asp.net mvc。我正在尝试首先将使用 EF 代码的迁移应用于我的项目。我正在使用 MySql 数据库。目前我使用的是 EF 4.3.1 版本和 6.6.4.0 版本的 mysql connector/.net。我成功地将迁移添加到我的项目中,这意味着我执行了一系列没有给我错误的命令。我已按照这些步骤操作,

PM> Enable-Migrations

PM> Add-Migration InitialMigration

PM> update-database -verbose

这些步骤为我的项目创建了 Migration 文件夹,并在 Migration 文件夹中创建了配置和 timestamp_Initialmigration 文件,在配置文件中我添加了以下代码。

SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());

之后,我在我的类(class)中添加了一个字段,例如,

public int newprop{get; set;}

之后我在 PM 控制台中执行 update-database -verbose 命令。

当我执行我的应用程序时,它会抛出一个错误,如

“‘字段列表’中的未知列‘Extent1.newprop’”

请指导我为什么会收到此错误,我是否走错了路请指导我。

最佳答案

如果您不使用自动迁移

Database.SetInitializer(new MigrateDatabaseToLatestVersion());

 public class MyMigrationConfiguration : DbMigrationsConfiguration<MyDbContext>
    {
        public MyMigrationConfiguration()
        {
            AutomaticMigrationsEnabled = true; // Are you using this ?????
            AutomaticMigrationDataLossAllowed = true;
        }
    }

然后您需要使用 PM Commandlet 告诉 EF 添加另一个迁移并更新数据库。

PM> Enable-Migrations  //already done this ?
PM> Add-Migration myLabel
PM> Update-Database

在网络上搜索“基于代码的迁移”以获取帮助

关于mysql - 无法将代码优先迁移应用到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13913126/

相关文章:

php - Laravel - 集合表

python - 如何选择 Python 列表框数据并将其填充到特定的实体框 - 预输入

mysql - "FIELDS TERMINATED BY"和 "LINES TERMINATED BY"有什么问题?

C# MVC 返回 View (对象)导致查询字符串?

c# - ASP.NET MVC/WEB API 文件上传 : Memory not being deallocated after upload

c# - 从字符串构建表达式<Func<T,bool>>

c# - 为什么 Entity Framework 没有在我的模型上正确保存枚举属性?

MySQL JSON : finding value of sibling element in sub-array

c# - EntityFramework 匿名复合键属性名称冲突

.net MVC 功能测试