C# SQLite EF6多对多级联删除

C# SQLite EF6多对多级联删除,c#,database,sqlite,entity-framework-6,C#,Database,Sqlite,Entity Framework 6,我有以下两门课: [Table("Products")] public class Product { public int Id { get; set; } public string Code { get; set; } public string Name { get; set; } public virtual ICollection<Process> Processes { get; set; } public Product(

我有以下两门课:

[Table("Products")]
public class Product
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }


    public virtual ICollection<Process> Processes { get; set; }

    public Product()
    {
        this.Processes = new HashSet<Process>();
    }
}
这将创建一个名为
ProcessProducts
的新表,其中存储了多对多关系

我现在的问题是,当我从数据库中删除例如Product时,我想自动删除
ProcessProducts
表中的所有行,其中使用了特定的
ProductId
。我想在modelBuilder下配置
CascadeDelete
,但它不允许我这样做

也许是这样,我是如何移除错误的项目的

    public void Delete(TEntity entity)
    {
        if (entity == null) throw new ArgumentNullException("entity");
        _context.Set<TEntity>().Attach(entity);
        _context.Set<TEntity>().Remove(entity);
        _context.SaveChanges();
    }
public void Delete(TEntity实体)
{
如果(entity==null)抛出新的ArgumentNullException(“entity”);
_context.Set().Attach(实体);
_context.Set().Remove(实体);
_SaveChanges();
}

非常感谢您在这方面提供的任何帮助。

在级联模式下删除,您可以使用或

每种情况都需要配置级联的深度。所以问题是:你想走多深:

如果您删除了一个
产品
应删除与其相关的de
流程
,如果该
流程
有其他
产品
应保留此删除链

可能通过级联删除,您只意味着删除
产品
过程
之间的连接(多对多关系)。如果是这种情况,您只需执行以下操作:

public void DeleteProduct(Product entity)
{
    if (entity == null) throw new ArgumentNullException("entity");
    entity = _context.Set<Product>().First(f => f.Id == entity.Id);
    _context.Set<Product>().Remove(entity);
    _context.SaveChanges();
}
公共产品(产品实体)
{
如果(entity==null)抛出新的ArgumentNullException(“entity”);
entity=\u context.Set().First(f=>f.Id==entity.Id);
_context.Set().Remove(实体);
_SaveChanges();
}
这将删除您的
产品
以及与您的
产品
连接的所有
ProcessProducts
注册表

    public void Delete(TEntity entity)
    {
        if (entity == null) throw new ArgumentNullException("entity");
        _context.Set<TEntity>().Attach(entity);
        _context.Set<TEntity>().Remove(entity);
        _context.SaveChanges();
    }
public void DeleteProduct(Product entity)
{
    if (entity == null) throw new ArgumentNullException("entity");
    entity = _context.Set<Product>().First(f => f.Id == entity.Id);
    _context.Set<Product>().Remove(entity);
    _context.SaveChanges();
}