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

查看:119
本文介绍了重命名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'.

我认为它试图删除数据,因为它需要列中的数据(不能为null),所以我看到了该错误。但我不想删除数据。我该如何重命名?

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)重命名实体之前,使用 ToTable HasColumnName 流利的API或数据注释。对引用实体的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)删除注释/流利的配置并执行实际的类/属性重命名:

(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 如果有:

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

,您就完成了。

您可以通过添加新迁移来进行验证-该迁移将具有空的 Up Down 方法。

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

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

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