2016-11-23 32 views
0

我需要關聯表來實現InspChars和材質之間的M:M關係,但應該有一個額外的字段。這就是爲什麼我創建另一個表InspCharsToMaterials。 一切正常,但是,如果創建了自己生成的自動生成的關聯表,我根本不需要它。如何禁止自動創建關聯表EF

如何應對?如何關閉ef的這種可能性(僅適用於多對多)。

[Table("tbl_Materials")] 
public class Material 
{ 
    [Key] 
    public int key { get; set; } 

    [Required] 
    [Column(TypeName = "NVARCHAR")] 
    [StringLength(18)] 
    public string MaterialID { get; set; } 

    [Required] 
    [Column(TypeName = "NVARCHAR")] 
    [StringLength(128)] 
    public string MaterialName { get; set; } 

    [Column(TypeName = "bit")] 
    public bool Deleted { get; set; } 
    public virtual ICollection<InspectionCharacteristic> InspCharLinks { get; set; } 
} 



[Table("tbl_InspectionCharacteristic")] 
public class InspectionCharacteristic 
{ 
    [Key] 
    [Required] 
    public int key { get; set; } 

    [Required] 
    [Column(TypeName = "NVARCHAR")] 
    [StringLength(40)] 
    public string InspCharID { get; set; } 

    [Required] 
    [Column(TypeName = "NVARCHAR")] 
    [StringLength(40)] 
    public string InspCharName { get; set; } 

    [Column(TypeName ="BIT")] 
    public bool IsCatalogType { get; set; } 

    /*[Column(TypeName = "NVARCHAR")] 
    [StringLength(8)] 
    public string CatalogGroupLink { get; set; }*/ 

    [Column("CatalogGroupID")] 
    public int? CatalogGroupKeyID { get; set; } 
    //public virtual CatalogGroup CatalogGroup { get; set; } 
    public virtual ICollection<Material> Materials { get; set; } 

} 

我的關聯表:

[Table("tbl_InspCharToMaterials")] 
public class InspCharToMaterial 
{ 
    [Key] 
    public int key { get; set; } 

    public virtual Material Material { get; set; } 
    public virtual InspectionCharacteristic InspectionCharacteristic { get; set; } 
    public bool Deleted { get; set; } 

} 
+0

可能的重複[首先創建代碼,多對多,關聯表中有附加字段](http://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional -fields-在關聯表) –

回答

1

MaterialInspectionCharacteristicpublic virtual ICollection<InspectionCharacteristic> InspCharLinks { get; set; }替換public virtual ICollection<Material> Materials { get; set; }public virtual ICollection<InspCharToMaterial> InspCharToMaterials { get; set; };