cs1061“collectioncollectionbuilder
using CafeApiProject.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CafeApiProject
{
public class AppDbContext : DbContext
{
DbSet<ProductCategory> ProductCategories { get; set; }
DbSet<Products> Products { get; set; }
DbSet<OrderProducts> OrderProducts { get; set; }
DbSet<Employees> Employees { get; set; }
DbSet<Orders> Orders { get; set; }
DbSet<Customers> customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=CafeDb;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Products>()
.HasOne(p => p.Category)
.WithMany(c => c.Products)
.HasForeignKey(p => p.CategoryId);
modelBuilder.Entity<OrderProducts>()
.HasMany(op => op.Product)
.WithMany(c => c.Orders)
.HasForeignKey(op => new { op.OrderId, op.ProductId });
modelBuilder.Entity<Orders>()
.HasOne(e => e.Employee)
.WithMany(c => c.Orders)
.HasForeignKey(e => e.EmployeeId);
modelBuilder.Entity<Orders>()
.HasOne(c => c.Customer)
.WithMany(c => c.Orders)
.HasForeignKey(p => p.CustomerId);
modelBuilder.Entity<Orders>()
.HasOne(p => p.Order)
.WithMany(c => c.Orders)
.HasForeignKey(p => p.OrderId );
}
}
}
namespace CafeApiProject.Models
{
public class Orders
{
[Key]
public Guid OrderId { get; set; }
[Required]
[ForeignKey("OrderId")]
public OrderProducts Order { get; set; }
public Guid CustomerId { get; set; }
[Required]
[ForeignKey("Id")]
public Customers Customer { get; set; }
public Guid EmployeeId { get; set; }
[Required]
[ForeignKey("EmployeeId")]
public Employees Employee { get; set; }
public string OrderList { get; set; }
}
}
namespace CafeApiProject.Models
{ [PrimaryKey(nameof(OrderId), nameof(ProductId))]
public class OrderProducts
{
public Guid OrderId { get; set; }
[Required]
public Guid ProductId { get; set; }
[ForeignKey("ProductId")]
public ICollection<Products> Product { get; set; }
public ICollection<Orders> Orders { get; set; }
}
}
namespace CafeApiProject.Models
{
public class Products
{
[Key]
public Guid ProductId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Article { get; set; }
[Required]
public int Price { get; set; }
public Guid CategoryId { get; set; }
[ForeignKey("CategoryId")]
public ProductCategory Category { get; set; }
public ICollection<OrderProducts> Orders { get; set; }
}
}
请帮忙 尝试添加迁移时也出现错误: “实体类型“OrderProducts”需要定义主键。如果您打算使用无键实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无键实体类型的更多信息,请参阅https://go。 microsoft.com/fwlink/?linkid=2141943。”
从您的实体类和 Fluent API 中,我看到生成
OrderProducts
实体存在很多问题,该实体是 Order
和 Product
实体之间多对多关系的关联表。
订单:
Orders
实体应该具有一对多OrderProducts
。
这似乎是一种冲突,您声明了
CustomerId
列,但您将 ForeignKey
属性与 Id
应用于相关的 Customer
属性。
订购产品:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Products>()
.HasOne(p => p.Category)
.WithMany(c => c.Products)
.HasForeignKey(p => p.CategoryId);
modelBuilder.Entity<Orders>()
.HasOne(e => e.Employee)
.WithMany(c => c.Orders)
.HasForeignKey(e => e.EmployeeId);
modelBuilder.Entity<Orders>()
.HasOne(c => c.Customer)
.WithMany(c => c.Orders)
.HasForeignKey(p => p.CustomerId);
modelBuilder.Entity<OrderProducts>()
.HasKey(op => new { op.OrderId, op.ProductId });
modelBuilder.Entity<OrderProducts>()
.HasOne(op => op.Product)
.WithMany(p => p.Orders)
.HasForeignKey(op => op.OrderId);
modelBuilder.Entity<OrderProducts>()
.HasOne(op => op.Order)
.WithMany(o => p.OrderProducts)
.HasForeignKey(op => op.OrderId);
}
public class Orders
{
[Key]
public Guid OrderId { get; set; }
public ICollection<OrderProducts> OrderProducts { get; set; }
[Required]
public Guid CustomerId { get; set; }
[ForeignKey("CustomerId")] // Id for storing CustomerId, really?
public Customers Customer { get; set; }
[Required]
public Guid EmployeeId { get; set; }
[ForeignKey("EmployeeId")]
public Employees Employee { get; set; }
public string OrderList { get; set; }
}
public class OrderProducts
{
[Required]
public Guid OrderId { get; set; }
[Required]
public Guid ProductId { get; set; }
public Products Product { get; set; }
public Orders Order { get; set; }
}
public class Products
{
[Key]
public Guid ProductId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Article { get; set; }
[Required]
public int Price { get; set; }
public Guid CategoryId { get; set; }
[ForeignKey("CategoryId")]
public ProductCategory Category { get; set; }
public ICollection<OrderProducts> Orders { get; set; }
}