简体   繁体   English

TPT实体框架的多级继承

[英]Multi-level Inheritance with TPT Entity Framework

I have the following: 我有以下内容:

public class Entity<TKey> : IEntity
{              
    [System.Runtime.Serialization.DataMemberAttribute]
    public virtual TKey Id { get; set; }
    [System.Runtime.Serialization.DataMemberAttribute]
    public virtual StateEnum State { get; set; }}


 public class Party : Entity<int>
{
    public virtual int TypeId { get; set; }
    public virtual string MobileNo1 { get; set; }}


public class Person : Party
{
    public virtual int PartyId { get; set; }
    public virtual string Name { get; set; }}

I have in sql Party table contains the following columns [Id,TypedId,MobileNo1] and Person table contains the following columns:[PartyId,Name] and PartyId is foreign key for Id of Party . 我在sql中的Party表包含以下列[Id,TypedId,MobileNo1],而Person表包含以下列:[PartyId,Name]和PartyId是Party ID的外键。

How to make the mapping ? 如何进行映射? I have tried but i'm not reach to any thing. 我已经尝试过,但是我什么都没做。

Edit 编辑

suppose that TKey is integer and the PartyId is key of person and foreign key to party 假设TKey是整数,PartyId是人员的密钥,而当事人的外键

I try to make map for person 我尝试为人制作地图

HasKey(p => p.PartyId); HasKey(p => p.PartyId);
ToTable("Person"); ToTable( “人”);

and to party 和聚会

   HasKey(p => p.Id);
            ToTable("Party");

and its not work, how I make mapping in this case ? 并且它不起作用,在这种情况下如何进行映射?

Edit 2: 编辑2:

I have the following 我有以下

 public class Entity<TKey> : IEntity
    {              
        [System.Runtime.Serialization.DataMemberAttribute]
        public virtual TKey Id { get; set; }
        [System.Runtime.Serialization.DataMemberAttribute]
        public virtual StateEnum State { get; set; }}


     public class Person : Entity<int>
    {
        public virtual string FirstName{ get; set; }
        public virtual string LastName{ get; set; }
}


    public class Employee: Person 
    {
        public virtual int PersonId { get; set; }
        public virtual string CompanyName{ get; set; }
}

I have Person table contain [Id,FirstName,LastName] I have Employee table contain [PersonId,FirstName,LastName] 我的Person表包含[Id,FirstName,LastName]我的Employee表包含[PersonId,FirstName,LastName]

-- PersonId is foreign key to person table. -PersonId是人员表的外键。 -- employee is a person the relationship is one to one. -员工是一个人的关系是一对一的。 --for future i can add Student entity , so Student is person -为了将来我可以添加Student实体,所以Student是个人

How I can make mapping ? 我如何进行映射?

I think you're trying to do something like this: 我认为您正在尝试执行以下操作:

public class Entity<TKey> : IEntity
{              
[System.Runtime.Serialization.DataMemberAttribute]
public TKey Id { get; set; }
[System.Runtime.Serialization.DataMemberAttribute]
public StateEnum State { get; set; }}


public class Party : Entity<int>
{
public int TypeId { get; set; }
public string MobileNo1 { get; set; }

public virtual Person Person {get; set;}
}


public class Person
{
public int PartyId { get; set; }
public string Name { get; set; }

public virtual Party Party {get; set;}
}

So Person has a party (if it's optional you declare int? ) and Party inherits from Entity. 所以Person有一个party(如果是可选的,则声明int? ),并且Party从Entity继承。 I also removed a lot of virtual declarations which probably make no sense (correct me if I'm wrong on some of them). 我还删除了许多可能没有意义的虚拟声明(如果我对某些声明有误,请更正我)。 The virtual keyword is used in Entity Framework to indicate a navigation property. 在实体框架中使用virtual关键字表示导航属性。 So those properties are "links" to other existing entities. 因此,这些属性是与其他现有实体的“链接”。 (Also assumed that PartyId is the primary key for Person ). (还假定PartyIdPerson的主键)。

I'm not sure this will work though because of the TKey primary key in Party (if you declare no primary key yourself EF automatically picks a property with "id" in the name) can be a different type and the foreignkey from Person is always an int ... Why can't you just declare the Id as int ? 我不确定这是否会起作用,因为在Party中使用了TKey主键(如果您自己声明没有主键,EF会自动选择名称中带有“ id”的属性)可以是其他类型,并且Person的外键始终是一个int ...为什么不能只将Id声明为int

EDIT: 1:1 relationship 编辑:1:1关系

modelBuilder.Entity<Person>()
            .ToTable("Person")
            .HasKey(p => p.PartyId)
            .HasRequired(p => p.Party)
            .WithRequiredDependent(p => p.Person)
            .Map(pers => pers.MapKey("PartyId");

That should normally work. 那应该正常工作。 It basically says Person needs to have a party and party needs a person. 它基本上说人需要一个聚会,而聚会需要一个人。 Because Person has the foreign key, it is the dependent. 因为Person具有外键,所以它是从属的。

You will also need to update your models without all the virtual properties and add virtual property to the corresponding entities (like in models I posted).It would be a lot easier to just use the attributes though and leave the fluent api as it is for this one: 您还需要在没有所有虚拟属性的情况下更新模型,并将虚拟属性添加到相应的实体中(例如我发布的模型中的模型),但是仅使用属性并保留流利的api会更容易这个:

public class Entity<TKey> : IEntity
{              
[System.Runtime.Serialization.DataMemberAttribute]
public TKey Id { get; set; }
[System.Runtime.Serialization.DataMemberAttribute]
public StateEnum State { get; set; }}


public class Party : Entity<int>
{
public int TypeId { get; set; }
public string MobileNo1 { get; set; }

public virtual Person Person {get; set;}
}


public class Person
{
[Key,ForeignKey("Party")]
public int PartyId { get; set; }
public string Name { get; set; }

public virtual Party Party {get; set;}
}

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

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