0

I need association table to realize the M:M relationship between InspChars and Materials, but there should be an additional field. That is why I create another table InspCharsToMaterials. Everything is working, but ef creates its own auto-generated association table that I don't need at all.

How to cope with it? How to switch off this possibility of ef (only for many-to-many).

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

}

And my association table:

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

}
V. Ivanova
  • 41
  • 5
  • Possible duplicate of [Create code first, many to many, with additional fields in association table](http://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table) – hyankov Nov 23 '16 at 06:54

1 Answers1

1

Replace public virtual ICollection<Material> Materials { get; set; } on InspectionCharacteristic and public virtual ICollection<InspectionCharacteristic> InspCharLinks { get; set; } on Material with public virtual ICollection<InspCharToMaterial> InspCharToMaterials { get; set; };

hyankov
  • 4,049
  • 1
  • 29
  • 46