简体   繁体   English

实体框架的继承和关系

[英]Entity Framework inheritance and relationship

I'm trying to implement a relationship(one-to-many) for my entities which use default TPH inheritance 我正在尝试为使用默认TPH继承的实体实现一个(一对多)关系

public abstract class base
{
        public int Id { get; set; }
        ...
}

public class X : base
{
        public ApplicationUser User { get; set; }
       ...
}

public class Y : base
{
        public ApplicationUser User { get; set; }
       ...
}

public class ApplicationUser
{
        public string Name { get; set;}
       ...
        public ICollection<X> classX { get; set; }
        public ICollection<Y> classY { get; set; }
}

Everything works, but the problem is that Entity Framework creates two columns in the base table - User_Id and User_Id1. 一切正常,但问题是实体框架在基表中创建了两列-User_Id和User_Id1。 How can I map it so that there is only one column for the foreign key (User_Id) and depending on the content of the record in the Discriminator column (created by EF) the foreign key would be assigned to the appropriate entity? 我如何映射它,以便外键(User_Id)只有一列,并且根据Discriminator列(由EF创建)中记录的内容,外键将分配给适当的实体?

How can I map it so that there is only one column for the foreign key (User_Id) and depending on the content of the record in the Discriminator column (created by EF) the foreign key would be assigned to the appropriate entity? 我如何映射它,以便外键(User_Id)只有一列,并且根据Discriminator列(由EF创建)中记录的内容,外键将分配给适当的实体?

You can't. 你不能 Thinking about it a bit I don't see any obvious reason why that feature couldn't be implemented. 对此进行一点思考,我看不出无法实现该功能的任何明显原因。 It just hasn't. 只是没有。

If you want X and Y to share an attribute, derive them both from and intermediate XYEntity type, and give ApplicationUser a single Navigation Property of type ICollection<XYEntity> . 如果希望X和Y共享属性,请从XYEntity类型和中间XYEntity类型派生它们,并为ApplicationUser提供一个类型为ICollection<XYEntity>的单个Navigation属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM