gpt4 book ai didi

entity-framework-6 - 'PK_dbo.EntityName' 不是约束。无法删除约束。EF6

转载 作者:行者123 更新时间:2023-12-01 13:42:14 25 4
gpt4 key购买 nike

我首先使用实体​​框架 6 代码。我有三个这样的实体:

public class Doctor
{
public string DoctorID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
}

public class ExpertiseDetails
{
[Key, Column(Order = 1)]
public short expertiseID { get; set; }
[Key , Column(Order = 2)]
public string DoctorID { get; set; }

[ForeignKey("expertiseID")]
public Expertise expertise { get; set; }
public Doctor doctor { get; set; }
}


public class Expertise
{
[Key]
public short expertiseID { get; set; }
public string expertiseTitle { get; set; }
}

我需要一个 one to many Expertise之间的关系和 Doctor ,当我在控制台中运行 update-database 语句时 nuGet此错误显示:
'PK_dbo.ExpertiseDetails' is not a constraint. Could not drop constraint

怎么了 ?

最佳答案

在重命名表架构后遇到了类似的问题,我通过使用 sql 显式删除 PK 名称来解决。

在我的情况下,在重命名表架构后更改表中的主键时出现问题:

原来的:

  • 带有 PK 名称的 dbo.LogEntries:PK_dbo.LogEntries

  • 第 1 步:架构更改:
  • PK 不变的 Logging.LogEntries

  • 第二步:PK变化
  • PK 更改:结果为 DropPrimaryKey("Logging.LogEntries");

  • 最后一行转换为 PK 名称: PK_Logging.LogEntries这是不存在的。

    修复 :
    一般来说,有几种方法可以解决这个问题。我在显式迁移中通过 sql 语句删除了 PK。
    Sql("ALTER TABLE [Logging].[LogEntries] DROP CONSTRAINT [PK_dbo.LogEntries]");

    被告知;我有完整的生产访问权限,以防它失败并且没有使用迁移来回滚到以前的状态。

    关于entity-framework-6 - 'PK_dbo.EntityName' 不是约束。无法删除约束。EF6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39058367/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com