简体   繁体   English

实体框架自定义导航属性

[英]Entity Framework Custom Navigational Properties

Is there a way to map below ClassA.BId to ClassB.BetaId ? 有没有一种方法可以将ClassA.BId下面的内容ClassA.BIdClassB.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. 但是, ClassBId必须保留为主键。

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.

相关问题 Entity Framework 6代码优先中的多维导航属性 - Multidimensional navigational properties in Entity Framework 6 Code First 实体框架中的Restsharp和关系/导航属性 - Restsharp and relations / navigational properties in Entity Framework 实体框架:导航属性-代码优先 - Entity Framework: Navigational properties - Code First 阻止实体框架为导航属性插入值 - Prevent Entity Framework to Insert Values for Navigational Properties 实体框架3-链式导航属性中的过滤器元素 - Entity Framework 3 - Filter Elements in Chained Navigational Properties 使用实体框架时如何填充导航属性 - how to populate navigational properties when using Entity Framework 实体框架4每个层次结构表 - 如何定义儿童的导航属性? - Entity Framework 4 Table Per Hierarchy - How To Define Navigational Properties On Children? 实体框架4.1。 从ID加载导航属性 - Entity Framework 4.1. Loading navigational properties from Ids 使用实体框架和 OData 查询的导航 DTO 属性 - Navigational DTO properties using Entity Framework with OData Queries 实体框架核心包括加载额外的导航属性 - Entity Framework Core include loading extra navigational properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM