我正在建立一个电子商务网站。我想存储每个购物车的数据。因此,我在购物车和产品之间建立了多对多的关系。
问题:我无法删除购物车或产品。
我希望能够在不删除产品的情况下删除购物车。
请帮我用FluentAPI
编写规则。
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public int StockCount { get; set; }
[Required]
public Category Category { get; set; }
[Required]
public Brand Brand { get; set; }
public ICollection<Media> Media { get; set; }
public ICollection<Comment> Comments { get; set; }
public ICollection<Cart> Carts { get; set; }
}
public class Cart
{
[Key]
public int CartId { get; set; }
public bool Active { get; set; }
public ICollection<Product> Products { get; set; }
[Required]
public UserObject User { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ??
}
答案 0 :(得分:0)
这应该对你有用
public class Cart
{
[Key]
public int CartId { get; set; }
public ICollection<Product> Products { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
}
...这是正确的映射配置:
modelBuilder.Entity<Cart>()
.HasMany(c => c.Products)
.WithMany()
.Map(m =>
{
m.MapLeftKey("CartId");
m.MapRightKey("ProductID");
m.ToTable("CartProducts");
});