Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除多对多关系的规则ef codefirst_C#_Asp.net_Entity Framework_Many To Many_Code First - Fatal编程技术网

C# 删除多对多关系的规则ef codefirst

C# 删除多对多关系的规则ef codefirst,c#,asp.net,entity-framework,many-to-many,code-first,C#,Asp.net,Entity Framework,Many To Many,Code First,我正在建立一个电子商务网站。我想存储创建的每个购物车的数据。因此,我在推车和产品之间建立了多对多关系 如果产品与购物车相关,则会显示错误 如果购物车与产品相关,它也会给出一个错误 问题:我无法删除购物车或产品 我希望能够删除购物车而不删除产品 请帮助我用FluentAPI编写规则 public class Product { public int ProductID { get; set; } public string ProductName { get; set; }

我正在建立一个电子商务网站。我想存储创建的每个购物车的数据。因此,我在推车和产品之间建立了多对多关系

  • 如果产品与购物车相关,则会显示错误
  • 如果购物车与产品相关,它也会给出一个错误
问题:我无法删除购物车或产品

我希望能够删除购物车而不删除产品

请帮助我用
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)
{
  // ??
}
公共类产品
{
public int ProductID{get;set;}
公共字符串ProductName{get;set;}
公共字符串说明{get;set;}
公共双价{get;set;}
public int StockCount{get;set;}
[必需]
公共类别{get;set;}
[必需]
公共品牌品牌{get;set;}
公共ICollection媒体{get;set;}
公共ICollection注释{get;set;}
公共ICollection Carts{get;set;}
}
公共类购物车
{
[关键]
公共int-CartId{get;set;}
公共bool活动{get;set;}
公共ICollection产品{get;set;}
[必需]
公共用户对象用户{get;set;}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
// ??
}

这应该适合您

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; }
}
公共类购物车
{
[关键]
公共int-CartId{get;set;}
公共ICollection产品{get;set;}
}
公共类产品
{
public int ProductID{get;set;}
公共字符串ProductName{get;set;}
}
。。。以下是正确的映射配置:

modelBuilder.Entity<Cart>()
    .HasMany(c => c.Products)
    .WithMany()
    .Map(m =>
    {
        m.MapLeftKey("CartId");
        m.MapRightKey("ProductID");
        m.ToTable("CartProducts");
    });
modelBuilder.Entity()
.HasMany(c=>c.Products)
.有很多
.Map(m=>
{
m、 MapLeftKey(“CartId”);
m、 MapRightKey(“产品ID”);
m、 ToTable(“CartProducts”);
});
和一个有用的资源: