简体   繁体   English

3个外键到一个表到一列

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

there are 2 entities: "A" and "B". 有2个实体:“ A”和“ B”。 在此处输入图片说明 In essence, "A" there are fields: 本质上,“ A”包含以下字段:

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

In essence, "B": B_id and other fields. 本质上,“ B”:B_id和其他字段。 Is it possible to make relationships to the EF and MS one to one, where "B1_id", "B2_id" and "B3_id" are external keys to one property B_id in table "B". 是否可以一对一地建立与EF和MS的关系,其中“ B1_id”,“ B2_id”和“ B3_id”是表“ B”中一个属性B_id的外部键。 If it is possible, then how can I name the navigation properties in class "A", I think there should be three? 如果可以的话,如何在类“ A”中命名导航属性,我认为应该有三个?

You could do something like: 您可以执行以下操作:

 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.

相关问题 对于一个表EF 6具有2个外键的表无数据 - No data for table with 2 foreign keys to one table EF 6 如何在一个表中创建两个指向某个表中同一列的外键? - How to Create two Foreign Keys in one Table pointing to the same Column in some table? 如果table1包含fk到table2的同一列的多个字段,如何从一个表创建多个外键到另一个表 - How to create multiple foreign keys from one table to another if table1 contains multiple fields with fk to the same column of table2 一个实体框架核心表的两个外键 - Two foreign keys to one Entity Framework Core table 实体框架核心数据库优先-到一个表的多个外键 - Entity Framework Core Database First - Multiple Foreign keys to one table Entity Framework Core - 代码优先 - 两个外键 - 一张表 - Entity Framework Core - Code First - Two Foreign Keys - One Table 在一个表中引用两个外键-SQL Server C# - Referencing two foreign keys in one table - SQL Server C# 一个实体的多个外键 - Multiple Foreign Keys to one entity 具有可空外键的一对一关系 - One-To-One relationship with nullable foreign keys 一个表中有2个相同的外键。 Fluent Api生成3个外键而不是2个 - 2 same foreign keys in one table. Fluent Api generate 3 foreign key instead of 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM