c# - Dependent 和 Principal 角色中的 Code First Foreign Key 错误

标签 c# mysql entity-framework ef-code-first

已经阅读了一段时间内关于此的其他 SO 帖子,但仍然有些地方没有意义。制作包含这两个类的 MVC 应用程序

public class ChecklistFilled
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int InternalChecklistFilledID { get; set; }
    public DateTime ChecklistFilledDate { get; set; }
    public int PersonnelID { get; set; }

    [ForeignKey("PersonnelID")]
    public virtual Personnel Personnel { get; set; }
    public int EquipmentID { get; set; }

    [ForeignKey("EquipmentID")]
    public virtual ICollection<Equipment> Equipment { get; set; }
    public virtual ItemsFilled ItemsFilled { get; set; }
}

public class ItemsFilled
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int InternalItemsFilledID { get; set; }
    public bool? DeviceCondition { get; set; }
    public int DeviceID { get; set; }

    [ForeignKey("DeviceID")]
    public virtual Device Device { get; set; }
    public int ChecklistFilledID { get; set; }

    [ForeignKey("ChecklistFilledID")]
    public virtual ICollection<ChecklistFilled> ChecklistFilled { get; set; }
}

还有来 self 的 DBContext 类的语句

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ItemsFilled>()
            .HasRequired(x => x.ChecklistFilled)
            .WithMany()
            .Map(x => x.MapKey("ChecklistFilledID"));

        base.OnModelCreating(modelBuilder);
    }

到目前为止,我发现很多帖子的解决方案都非常针对程序员的问题,但没有任何内容足够直截了当让我作为新开发人员在这里申请。从我在 DBContext 设置中看到的,我根据需要将外键从 ChecklistFilled 类映射到 ItemsFilled 类,但是当我运行 enable-migrations 命令时,我得到错误

ItemsFilled_Device_Target_ItemsFilled_Device_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical

从表面上看,这试图告诉我我必须在每个类中拥有相同数量的属性——这对我来说听起来很荒谬。所以我不确定我需要做什么来解决这个问题并将 ChecklistID 作为 FK 放入 ItemsFilled 表中

最佳答案

如果我没理解错的话,您想要的是一种关系,其中 list 与项目列表相关,而项目与一个 list 相关。

要实现这种一对多关系,您应该像这样重新定义实体中的属性:

public class ChecklistFilled
{
    ...

    public virtual ICollection<ItemsFilled> ItemsFilled { get; set; }
}

public class ItemsFilled
{
    ...

    public int CheckListFilledId { get; set; }

    [ForeignKey("CheckListFilledId")]
    public virtual ChecklistFilled ChecklistFilled { get; set; }
}

通过此更改,您甚至不需要在 OnModelInitialize 中进行显式映射。事实上,您应该删除映射。

作为旁注,一般来说,如果 base.OnModelCreating(modelBuilder); 行是方法中的第一行。

迁移生成的表如下所示(为清楚起见,我删除了一些不相关的属性):

        CreateTable(
            "dbo.ChecklistFilleds",
            c => new
                {
                    InternalChecklistFilledID = c.Int(nullable: false, identity: true),
                    ChecklistFilledDate = c.DateTime(nullable: false),
                })
            .PrimaryKey(t => t.InternalChecklistFilledID);

        CreateTable(
            "dbo.ItemsFilleds",
            c => new
                {
                    InternalItemsFilledID = c.Int(nullable: false, identity: true),
                    DeviceCondition = c.Boolean(),
                    CheckListFilledId = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.InternalItemsFilledID)
            .ForeignKey("dbo.ChecklistFilleds", t => t.CheckListFilledId, cascadeDelete: true)
            .Index(t => t.CheckListFilledId);

也许你应该考虑重命名你的实体,名称有点困惑,特别是当复数形式组成表名称时。我建议使用 FilledCheckListFilledItem

此外,您收到的错误指出有问题的关系是 ItemsFilled_Device_Target_ItemsFilled_Device_Source。它是关于 DevicesItemsFilled 的。您的代码似乎在导致问题的 Device 实体中包含一些注释或映射。

关于c# - Dependent 和 Principal 角色中的 Code First Foreign Key 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293375/

相关文章:

c# - query.wait 没有完成

c# - 我应该使用 WCF 还是原始套接字?

python - 尝试在 Mac OS X Yosemite 上安装 MySQL-python 时出错

MySQL 注册器数据使用 setString 数据,但不使用 getParameter

java - 我可以在 JDBC 连接器中使用数据库 IP 地址而不是主机名吗?

.net - Entity Framework 4.1 对 TPT 和 TPCT 模型进行了更新?

linq-to-sql - L2S(LINQ to SQL)或 EF( Entity Framework )

c# - 在实例化对象时以编程方式使用字符串作为对象名称

c# - 如何引用实现某个接口(interface)的类?

C# 字典性能