c# - Create relationships with Entity Framework Core -


i want build database entity framework core. use command prompt , migrations create database. can see on diagram, have many-to-many relationship. how create relationship classes below?

enter image description here

code:

public class shoppingdbcontext : identitydbcontext<user> {     public shoppingdbcontext(dbcontextoptions options) : base(options)     {     }      protected override void onconfiguring(dbcontextoptionsbuilder     optionsbuilder)     {         base.onconfiguring(optionsbuilder);     }      public dbset<order> orders { get; set; }     public dbset<product> products { get; set; }     public dbset<partcategory> partcategory { get; set; }     public dbset<part> parts { get; set; } }  public class product  {     public int productid { get; set; }     public string productname { get; set; }     public double price { get; set; }      public list<partcategory> partcategory { get; set; }  }  public class partcategory {     public int partcategoryid { get; set; }     public string category { get; set; }      public list<part> parts { get; set; } } 

//update

public class productpartcategory { public int productid { get; set; } public product product { get; set; }  public int partcategoryid { get; set; } public partcategory partcategory { get; set; } }  public class product  {  public int productid { get; set; } public string productname { get; set; } public double price { get; set; } public list<partcategory> partcategories{ get; set; }   }   public class partcategory { public int partcategoryid { get; set; } public string category { get; set; } public list<product> products { get; set; }  //dont mind propertie other stuff public list<part> parts { get; set; } } 

you can try shown below using fluent api.

note :

many-to-many relationships without entity class represent join table not yet supported. however, can represent many-to-many relationship including entity class join table , mapping 2 separate one-to-many relationships.

public class shoppingdbcontext: dbcontext     {         public dbset<product> products { get; set; }         public dbset<partcategory> partcategories{ get; set; }          protected override void onmodelcreating(modelbuilder modelbuilder)         {             modelbuilder.entity<productpartcategory>()                 .haskey(t => new { t.productid, t.partcategoryid });              modelbuilder.entity<productpartcategory>()                 .hasone(pt => pt.product)                 .withmany(p => p.productpartcategories)                 .hasforeignkey(pt => pt.productid);              modelbuilder.entity<productpartcategory>()                 .hasone(pt => pt.partcategory)                 .withmany(t => t.productpartcategories)                 .hasforeignkey(pt => pt.partcategoryid);         }     } 

your models should :

 public class product    {      public int productid { get; set; }     public string productname { get; set; }     public double price { get; set; }     public list<productpartcategory> productpartcategories { get; set; }     }   public class partcategory    {      public int partcategoryid { get; set; }      public string category { get; set; }      public list<productpartcategory> productpartcategories { get; set; }     }    public class productpartcategory     {       public int productid { get; set; }       public product product { get; set; }        public int partcategoryid { get; set; }       public partcategory partcategory { get; set; }    } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -