簡體   English   中英

3個外鍵到一個表到一列

[英]3 foreign keys to one table to one column

有2個實體:“ A”和“ B”。 在此處輸入圖片說明 本質上,“ A”包含以下字段:

 - A_id (PK)
 - B1_id (FK)
 - B2_id (FK)
 - B3_id (FK)

本質上,“ B”:B_id和其他字段。 是否可以一對一地建立與EF和MS的關系,其中“ B1_id”,“ B2_id”和“ B3_id”是表“ B”中一個屬性B_id的外部鍵。 如果可以的話,如何在類“ A”中命名導航屬性,我認為應該有三個?

您可以執行以下操作:

 public class A
    {
        public int Id { get; set; }

        public int B1Id { get; set; }
        public int B2Id { get; set; }
        public int B3Id { get; set; }

        [ForeignKey("B1Id")]
        public B B1 { get; set; }

        [ForeignKey("B2Id")]
        public B B2 { get; set; }

        [ForeignKey("B3Id")]
        public B B3 { get; set; }
    }

    public class B
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }
public class A
{
    public int Id { get; set; }
    //another properties

    public virtual B B1 { get; set; }
    public int B1Id { get; set; }

    public virtual B B2 { get; set; }
    public int B2Id { get; set; }

    public virtual B B3 { get; set; }
    public int B3Id { get; set; }
}

public class B
{
    public int Id { get; set; }
    //another properties

    [InverseProperty(B1)]
    public virtual ICollection<A> A1 { get; set; }
    [InverseProperty(B2)]
    public virtual ICollection<A> A2 { get; set; }
    [InverseProperty(B3)]
    public virtual ICollection<A> A3 { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM