我想用Entity Framework Core建立一個數據庫。我使用命令提示符和遷移來創建數據庫。但正如你在我的圖表中看到的,我有一個多對多的關係。我如何在下面創建與我的課程的關係?與Entity Framework Core建立關係
代碼:
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; }
}
//更新
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 this propertie its for other stuff
public List<Part> Parts { get; set; }
}
你應該閱讀文檔:https://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many –
的可能的複製[如何創建一個多對多的關係與EF7的最新每晚構建?](http://stackoverflow.com/questions/29442493/how-to-create-a-many-to-many-relationship-with-latest-nightly-builds-of-ef7) – tmg