英文:
Entity Framework Core fails to rename database column
问题
以下是您要翻译的内容:
每当我修改我的实体之一并使用“Add-Migration
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 <name>
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: "OldName",
table: "MyTable",
newName: "NewName");
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论