[英]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.