Entity Framework Core 无法重命名数据库列

huangapple go评论76阅读模式
英文:

Entity Framework Core fails to rename database column

问题

以下是您要翻译的内容:

每当我修改我的实体之一并使用“Add-Migration ”在Package Manager控制台中生成迁移时,如果生成的MigrationBuilder包含重命名列操作,例如:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.RenameColumn(
        name: "OldName",
        table: "MyTable",
        newName: "NewName");
}

然后,“Update-Database”操作会失败,并显示以下错误:

对象引用未设置为对象的实例。

应用迁移 '20230506191317_mymigration'。
System.NullReferenceException: 对象引用未设置为对象的实例。

在MySql.EntityFrameworkCore.Migrations.MySQLMigrationsSqlGenerator.Generate(RenameColumnOperation operation, IModel model, MigrationCommandListBuilder builder) 中
在Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__82_22(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b) 中
在[...]
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) 中

只有在重命名列时才会发生这种情况。从迁移中删除该命令并运行迁移是有效的。ModelSnapshot没有不同步。

我要么必须删除我的迁移并重新开始,要么在MySQL Workbench中手动重命名表。

我使用.NET 7.0和Entity Framework Core 7.0.5与MySQL。您有什么想法吗?谢谢。

英文:

Whenever I modify one of my entities and generate a migration using Add-Migration &lt;name&gt; in the Package Manager Console, if the generated MigrationBuilder contains a rename column operation such as:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.RenameColumn(
        name: &quot;OldName&quot;,
        table: &quot;MyTable&quot;,
        newName: &quot;NewName&quot;);
}

then the Update-Database operation fails with the following error:

> Object reference not set to an instance of an object.
>
> Applying migration '20230506191317_mymigration'.
System.NullReferenceException: Object reference not set to an instance of an object.
>
> at MySql.EntityFrameworkCore.Migrations.MySQLMigrationsSqlGenerator.Generate(RenameColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
> at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__82_22(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
> at [...]
> at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

It only happens when renaming columns. Removing that command from the migration and running the migration works. The ModelSnapshot is not out of sync.

I either have to nuke my migrations and start fresh or run the migration without the rename and manually rename the table in MySQL workbench.

I'm using .Net 7.0 and Entity Framework Core 7.0.5 with MySQL.

Any ideas? Thanks.

答案1

得分: 1

I saw from this question that MySql.EntityFramework.Core has some issues, and to switch to Pomelo. I tried that, also specifying the right MySqlServerVersion version in the options when calling UseMySql and it now works. It also fixed other issues I was running into.

英文:

I saw from this question that MySql.EntityFramework.Core has some issues, and to switch to Pomelo. I tried that, also specifying the right MySqlServerVersion version in the options when calling UseMySql and it now works. It also fixed other issues I was running into.

huangapple
  • 本文由 发表于 2023年5月7日 03:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定