Entity Framework : Code First Approach. Creating Entities using TPT inheritance

China☆狼群 提交于 2019-12-11 17:14:46

问题


I am new to entity framework and I am using code first approach to create entities using TPT inheritance.

My requirement is to create the entities as per the attached diagram where ID is PK for Customers table and FK for the AddressDetails and ContactDetails table. Based on the keys I also need to create the association and navigation properties for the entities. Table Diagram

In my code I have created entities as

public class Customer
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int ZipCode { get; set; }
    public virtual ContactDetails ContactDetails { get; set; }
    public virtual AddressDetails AddressDetails { get; set; }
}

[Table("ContactDetails")]
public class ContactDetails: Customer
{
     public string MobileNo { get; set; }
     public string EmailId { get; set; }        
}

[Table("AddressDetails")]
public class AddressDetails: Customer
{
     public string BillingAddress { get; set; }
     public string DeliveryAddress { get; set; }
}

My question is, have I created the association and navigation properties correctly or do I need to add them in the ContactDetails and AddressDetails class as well? Also, when I run the code the entities are getting created in the database but for the Customer table there are 2 additional columns created as AddressDetails_Id(FK,int,null) and ContactDetails_Id(FK,int,null). I think they are created because of the navigation property but I do not need these columns in the database to be created. Also the values are always null in these two columns.

Any help would be appreciated. Thanks in advance.

来源:https://stackoverflow.com/questions/47274737/entity-framework-code-first-approach-creating-entities-using-tpt-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!