实体框架:代码优先方法。使用TPT继承创建实体

时间:2017-11-13 22:38:38

标签: entity-framework ef-code-first

我是实体框架的新手,我使用代码优先方法来创建使用TPT继承的实体。

我的要求是按照附图创建实体,其中ID为Customers表的PK,AddressDetails和ContactDetails表的FK。基于键我还需要为实体创建关联和导航属性。 Table Diagram

在我的代码中,我创建了实体

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; }
}

我的问题是,我是否正确创建了关联和导航属性,还是需要在ContactDetails和AddressDetails类中添加它们?此外,当我运行代码时,实体将在数据库中创建,但对于Customer表,还有2个附加列创建为AddressDetails_Id(FK,int,null)和ContactDetails_Id(FK,int,null)。我认为它们是由于导航属性而创建的,但我不需要在数据库中创建这些列。此外,这两列中的值始终为空。

任何帮助将不胜感激。提前谢谢。

0 个答案:

没有答案