当多个属性引用导航属性EF Code First时出错

时间:2020-07-06 15:11:00

标签: c# asp.net-core ef-core-3.1

在任何模型设计中,一个非常常见的任务是让多个外键指向同一个外表。我首先在ef核心3.1中使用EF代码,但是有多个外键存在问题。

但是,当我尝试以下操作

public class MISObject {  

    [ForeignKey("StandardLookups"), Column(Order = 1)]
    public int? Enf_Item { get; set; }

    [ForeignKey("StandardLookups"), Column(Order = 1)]
    public int? Enf_Type { get; set; }

    [ForeignKey("StandardLookups"), Column(Order = 1)]
    public int? Enf_Cat { get; set; }

    public virtual MISStandardLookups StandardLookups{ get; set; }
}

当我尝试在EF Core 3.1中运行迁移时出现以下错误,我需要3个键的原因是它们是三个相互链接的下拉菜单。肯定是依赖项下拉列表,您应该能够将3个外键指向同一个外表。

有多个属性指向导航“ StandardLookups” 在实体类型“ MISObject”中。使用数据定义复合外键 批注,请在导航上使用ForeignKeyAttribute。

 public class MISStandardLookups : BaseModel {
    public int Id { get; set; }
    public int LookupGroup { get; set; }
    public int LookupSubGroup { get; set; }
    public string LookupDescription { get; set; }
    public string   LookupText { get; set; }
    public string  LookupValue { get; set; }
}

感谢您的帮助

顺便说一句,我不想​​拥有三个差异虚拟属性,例如

public virtual MISStandardLookups StandardLookupsEnfItem{ get; set; }
public virtual MISStandardLookups StandardLookupsEnfType{ get; set; }
public virtual MISStandardLookups StandardLookupsEnfCat{ get; set; }

有时候EF可以如此强大,但是却以简单的方式落在脸上。

1 个答案:

答案 0 :(得分:0)

您的模型应如下所示:

public class MisStandardLookups
{
    public int ID { get; set; }
    public int LookupGroup { get; set; }
    public int LookupSubGroup { get; set; }
    public string LookupDescription { get; set; }
    public string LookupText { get; set; }
    public string LookupValue { get; set; }

    public virtual ICollection<MisObject> MISObjectsItems { get; set; }
    public virtual ICollection<MisObject> MISObjectsTypes { get; set; }
    public virtual ICollection<MisObject> MISObjectsCats { get; set; }
}

public class MisObject
{
    public int ID { get; set; }

    [ForeignKey("StandardLookups"), Column(Order = 1)]
    public int? Enf_ItemID { get; set; }

    [ForeignKey("StandardLookups"), Column(Order = 1)]
    public int? Enf_TypeID { get; set; }

    [ForeignKey("StandardLookups"), Column(Order = 1)]
    public int? Enf_CatID { get; set; }

    public virtual MisStandardLookups StandardLookupsItem { get; set; }
    public virtual MisStandardLookups StandardLookupsType { get; set; }
    public virtual MisStandardLookups StandardLookupsCat { get; set; }
}

然后在Fluent API中,将关系定义为:

protected override void OnModelCreating(ModelBuilder builder)
 {
     builder.Entity<MisObject>()
            .HasOne(x => x.StandardLookupsCat)
            .WithMany(x => x.MISObjectsCats)
            .HasForeignKey(x => x.Enf_CatID);

        builder.Entity<MisObject>()
            .HasOne(x => x.StandardLookupsItem)
            .WithMany(x => x.MISObjectsItems)
            .HasForeignKey(x => x.Enf_ItemID);

        builder.Entity<MisObject>()
            .HasOne(x => x.StandardLookupsType)
            .WithMany(x => x.MISObjectsTypes)
            .HasForeignKey(x => x.Enf_TypeID);

        base.OnModelCreating(builder);
 }

您说:

顺便说一句,我不想​​拥有三个diff虚拟属性。

StandardLookupsCat表的记录已填充多个FK时,EF应该如何知道MisObject的哪个记录指向?

希望这会有所帮助。