2013-06-29 46 views
0

基本上我創建了一個父變量的實體,我顯然希望將其包含在存儲實體的表中。EF中的繼承 - 如何在實體中包含父變量

這裏是父母的定義:

public class ProtectedProperty 
    { 
     [Key] 
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
     public int PropertyId { get; set; } 

     [Required] 
     public string SubId { get; set; } 

     public int Downloads { get; set; } 

     [Required] 
     public UserProfile Owner { get; set; } 

     [Required] 
     public string Name { get; set; } 

     [Required] 
     public ProtectedPropertyType Type { get; set; } 
    } 

下面是從父母繼承的財產(這其中有一個數據庫中的表):

[Table("ProtectedPassword")] 
    public class ProtectedPassword : ProtectedProperty 
    { 

     [Required] 
     //[StringLength(maximumLength:56)] 
     [MinLength(3)] 
     [MaxLength(20)] 
     public string Password { get; set; } 

     public ProtectedPassword(string name, UserProfile owner, string password) 
     { 
      Name = name; 
      Owner = owner; 
      Password = password; 
      SubId = PublicUtility.GenerateRandomString(8, 0); 
      Type = ProtectedPropertyType.Password; 
     } 

問題然而這當我查看數據庫中的表定義時,我只能看到ProtectedPassword的2個屬性: Password和PropertyId

我想要其他變量(包括所有者,姓名等)

回答

1

簡短回答:刪除[Table]屬性。

它導致您的模型中的繼承映射通過Table-Per-Type (TPT)。通過將基類和派生類的TPT屬性分成兩個單獨的表。

如果你想在一個表中的所有屬性,你有兩個選擇:

  • 隨着Table-Per-Hierarchy (TPH)基類的所有屬性和所有派生類將被存儲在一個表。 TPH是默認繼承映射,如果您未添加[Table]屬性或使用Fluent API進行顯式映射,將應用此默認繼承映射。

  • With Table-Per-Concrete-Type (TPC)繼承層次結構中的每個具體類型都有自己的表,它將在繼承鏈中保存其屬性及其基類的屬性。其實在你的情況下,這個映射不會有幫助,因爲你的基類也是具體的,而不是abstract