在 Entity Framework Core 中重命名外键而不删除数据 [英] Rename a foreign key in Entity Framework Core without dropping data

查看:22
本文介绍了在 Entity Framework Core 中重命名外键而不删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型类:

public class Survey
{
    public int SurveyId { get; set; }
    public string Name { get; set; }
}

public class User
{
    public int UserId { get; set; }

    public int SurveyId { get; set; }
    public Survey Survey { get; set; }
}

我想将Survey 重命名为StudentSurvey,这样它就会有StudentSurveyId.我相应地更新了模型中的类名和属性并添加了迁移.

I want to rename Survey to StudentSurvey, so it will have StudentSurveyId. I update the class name and the properties in the models accordingly and add a migration.

但是,我得到:

ALTER TABLE 语句与 FOREIGN KEY 约束FK_User_Surveys_SurveyId"冲突.数据库AppName"、表dbo.Survey"、SurveyId"列发生冲突.

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_User_Surveys_SurveyId". The conflict occurred in database "AppName", table "dbo.Survey", column 'SurveyId'.

我认为它正在尝试删除数据,因为它需要列中的数据(不能为空),所以我看到了该错误.但我不想丢弃数据.我怎样才能重命名它?

I think it is trying to drop the data and because it requires data in the column (cannot be null) I'm seeing that error. But I don't want to drop the data. How can I just rename it?

推荐答案

EF Core 将实体类重命名视为删除旧实体并添加新实体,因此生成迁移以删除原始表并创建新表.

EF Core treats the entity class rename as deleting the old entity and adding a new entity, hence generates a migration to drop the original table and create a new one.

解决方法需要以下步骤:

The workaround requires the following steps:

(1) 在重命名实体之前,使用ToTableHasColumnName fluent API 或数据注释重命名"表和PK 列.也对引用实体的 FK 列执行相同的操作.

(1) Before renaming the entity, "rename" the table and the PK column by using either ToTable and HasColumnName fluent API or data annotations. Also do the same for FK columns referencing the entity.

例如:

[Table("StudentSurveys")]
public class Survey
{
    [Column("StudentSurveyId")]
    public int SurveyId { get; set; }
    public string Name { get; set; }
}

public class User
{
    public int UserId { get; set; }
    [Column("StudentSurveyId")]
    public int SurveyId { get; set; }
    public Survey Survey { get; set; }
}

(2) 添加新的迁移.它将正确重命名表、PK 列、FK 列和关联的约束:

(2) Add new migration. It will correctly rename the table, PK column, FK columns and the associated constraints:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropForeignKey(
        name: "FK_Users_Surveys_SurveyId",
        table: "Users");

    migrationBuilder.DropPrimaryKey(
        name: "PK_Surveys",
        table: "Surveys");

    migrationBuilder.RenameTable(
        name: "Surveys",
        newName: "StudentSurveys");

    migrationBuilder.RenameColumn(
        name: "SurveyId",
        table: "Users",
        newName: "StudentSurveyId");

    migrationBuilder.RenameIndex(
        name: "IX_Users_SurveyId",
        table: "Users",
        newName: "IX_Users_StudentSurveyId");

    migrationBuilder.RenameColumn(
        name: "SurveyId",
        table: "StudentSurveys",
        newName: "StudentSurveyId");

    migrationBuilder.AddPrimaryKey(
        name: "PK_StudentSurveys",
        table: "StudentSurveys",
        column: "StudentSurveyId");

    migrationBuilder.AddForeignKey(
        name: "FK_Users_StudentSurveys_StudentSurveyId",
        table: "Users",
        column: "StudentSurveyId",
        principalTable: "StudentSurveys",
        principalColumn: "StudentSurveyId",
        onDelete: ReferentialAction.Cascade);
}

(3) 去掉注解/fluent配置,做实际的类/属性重命名:

(3) Remove the annotations / fluent configuration and do the actual class/property rename:

public class StudentSurvey
{
    public int StudentSurveyId { get; set; }
    public string Name { get; set; }
}

public class User
{
    public int SurveyUserId { get; set; }
    public int StudentSurveyId { get; set; }
    public StudentSurvey StudentSurvey { get; set; }
}

重命名相应的DbSet(如果有):

rename the corresponding DbSet if any:

public DbSet<StudentSurvey> StudentSurveys { get; set; }

你就完成了.

您可以通过添加新迁移来验证这一点 - 它将具有空的 UpDown 方法.

You can verify that by adding a new migration - it will have empty Up and Down methods.

这篇关于在 Entity Framework Core 中重命名外键而不删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆