[英]Entity Framework Custom Navigational Properties
Is there a way to map below ClassA.BId
to ClassB.BetaId
? 有没有一种方法可以将
ClassA.BId
下面的内容ClassA.BId
到ClassB.BetaId
? BetaId
in ClassB
is not primary key. ClassB
BetaId
不是主键。 Thus, mapping in following way end up in " The ForeignKeyAttribute is not valid" exception. 因此,以下列方式进行的映射最终导致“ ForeignKeyAttribute无效”异常。 Note that there is no foreign key relationship in these 2 classes.
请注意,在这两个类中没有外键关系。 For some reason I must not map
ClassA.BId
to ClassB.Id
because these 2 field is unrelated but I need to custom map ClassA.BId
to ClassB.BetaId
due to these 2 field is related. 由于某些原因,我不能将
ClassA.BId
映射到ClassB.Id
因为这两个字段无关,但是由于这两个字段是相关的,我需要自定义将ClassA.BId
映射到ClassB.BetaId
。 However, The Id
in ClassB
must remain as primary key. 但是,
ClassB
的Id
必须保留为主键。
Note: I'm using Entity Framework 6 注意:我正在使用Entity Framework 6
[Table("A")]
public class ClassA{
[Key]
public int Id { get; set; }
public int BId { get; set; }
[ForeignKey("BId")]
public virtual B B { get; set; }
}
[Table("B")]
public class ClassB{
[Key]
public int Id { get; set; }
public int BetaId { get; set; }
}
If B.BetaID is unique you can declare it to be the Key. 如果B.BetaID是唯一的,则可以将其声明为密钥。 Otherwise EF Core supports Foreign Key properties referencing Alternate Keys.
否则,EF Core支持引用备用键的外键属性。 See https://docs.microsoft.com/en-us/ef/core/modeling/alternate-keys
请参阅https://docs.microsoft.com/zh-cn/ef/core/modeling/alternate-keys
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.