2017-04-22 102 views
1

如果有人能告訴我爲什麼實體框架不爲以下模型創建連接表,我將不勝感激。它正在創建類型和功能的表格,但不是將會加入它們的表格。實體框架不創建連接表

public class DeviceType 
    { 
     [Display(Name = "ID")] 
     public int DeviceTypeID { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 

     public IEnumerable<DeviceFeature> DeviceFeatures { get; set; } 
    } 

    public class DeviceFeature 
    { 
     [Display(Name = "ID")] 
     public int DeviceFeatureID { get; set; } 

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

     public IEnumerable<DeviceType> DeviceTypes { get; set; } 

    } 

    public class DeviceFeatureView 
    { 
     public virtual IEnumerable<DeviceType> DeviceTypes { get; set; } 
     public virtual IEnumerable<DeviceFeature> DeviceFeatures { get; set; 
    } 
+0

在兩個實體類中將'IEnumerable '更改爲'ICollection '。 'ICollection '是EF集合導航屬性的最低要求。 –

回答

1

您不需要橋樑來創建多對多關係。 EF會解決它。更改導航屬性的類型從IEnumerableICollection這樣的:

public class DeviceType 
{ 
    public DeviceType() 
    { 
     this.DeviceFeatures = new HashSet<DeviceFeature>(); 
    } 
    [Display(Name = "ID")] 
    public int DeviceTypeID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 

    public ICollection<DeviceFeature> DeviceFeatures { get; set; } 
} 

public class DeviceFeature 
{ 
    public DeviceFeature() 
    { 
     this.DeviceTypes = new HashSet<DeviceType>(); 
    } 
    [Display(Name = "ID")] 
    public int DeviceFeatureID { get; set; } 

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

    public ICollection<DeviceType> DeviceTypes { get; set; } 

} 

更多關於它here

+0

謝謝CodingYoshi,我可能從來沒有想過那麼快。它正在工作 – Tom