1

我想用Entity Framework Core建立一個數據庫。我使用命令提示符和遷移來創建數據庫。但正如你在我的圖表中看到的,我有一個多對多的關係。我如何在下面創建與我的課程的關係?與Entity Framework Core建立關係

enter image description here

代碼:

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; } 
} 
+0

你應該閱讀文檔:https://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many –

+0

的可能的複製[如何創建一個多對多的關係與EF7的最新每晚構建?](http://stackoverflow.com/questions/29442493/how-to-create-a-many-to-many-relationship-with-latest-nightly-builds-of-ef7) – tmg

回答

0

你可以嘗試如下所示,使用流利的API

注:沒有一個實體類來表示 連接表

許多一對多的關係尚不支持。但是,您可以通過包含表的連接 的實體類並映射兩個單獨的一對多關係來表示多對多關係。

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

模式應該是這樣的:

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; } 
    } 
+0

我用你的代碼。但是,當我啓動網絡應用程序時得到這個錯誤信息....你可以在鏈接(https://gyazo.com/cb7df90ddc75c31db451a5cb17e2acf0) –

+0

你的代碼不是我的one.try我的代碼。如上所示。 'modelBuilder.Entity () .HasOne(pt => pt.Product) .WithMany(p => p.PartCategories) .HasForeignKey(pt => pt.ProductId);' – Sampath

+0

使用精確代碼,其中I包含模型類。您必須像我的代碼一樣更改您的模型。 – Sampath

0

你已經找到了一些答案,但這裏是你如何能找到自己......

我回答所有EF建模問題的方式是讓dotnet對我的數據結構進行逆向工程。通過在命令行提示符(從項目文件目錄內)執行腳手架命令,dotnet將創建一堆文件,然後您可以直接使用它或檢查結構並根據自己的喜好進行刪除。

這將工作的唯一方法是,如果你有你的開發數據庫中正確設置所有的關係(外鍵等)。

dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -a -t PartCategory -t Products -t ProducsPartCategory --o OutputDirectory