删除实体框架6中的连接表中的条目

时间:2022-08-31 01:57:08

This is a question about Entity Framework (EF) version 6.

这是关于实体框架(EF)版本6的问题。

I have three tables. TableA, TableB and TableJ like so:

我有三张桌子。 TableA,TableB和TableJ如下:

TableA
int Id (primary key)

TableB
int Id (primary key)

TableJ
int TableAId (primary key)
int TableBId (primary key)

Table J is a junction/mapping/many-to-many table between Table A and Table B. Note that there are no foreign keys. Mapping these tables in EF, I have the following code:

表J是表A和表B之间的联结/映射/多对多表。请注意,没有外键。在EF中映射这些表,我有以下代码:

public partial class A
{
    public int Id { get; set; }
    public virtual DbSet<B> Bs { get; set; }
}

public partial class B
{
    public int Id { get; set; }
    public virtual DbSet<A> As { get; set; }
}

The problem I'm facing is that I don't know how to delete the entries in the junction/mapping/many-to-many table. For example, this code doesn't delete anything:

我面临的问题是我不知道如何删除联结/映射/多对多表中的条目。例如,此代码不会删除任何内容:

var a = db.As.Find(id);
a.Bs.Clear();
db.SaveChanges();

What do I need to do to delete the entries in the junction/mapping/many-to-many table?

删除联结/映射/多对多表中的条目需要做什么?

1 个解决方案

#1


1  

Clear() doesn't actually have any effect on the object from the perspective of the context, it just clears the in-memory collection of any existing elements; instead you would do something like this:

从上下文的角度来看,Clear()实际上对对象没有任何影响,它只是清除任何现有元素的内存中集合;相反,你会做这样的事情:

var a = db.As.Find(id);
var removals = a.Bs.ToList(); //or you could filter to only remove B objects matching a specific criteria, etc.
foreach (var remove in removals)
{
   a.Bs.Remove(remove);
}
db.SaveChanges();

This materialises all of Bs in a and then removes each one from the a.Bs collection - then when you SaveChanges the objects will be deleted from the database.

这将实现a中的所有B,然后从a.Bs集合中删除每个B - 然后当您SaveChanges时,将从数据库中删除对象。

Incidentally, the reason we materialise first is because we can't both enumerate and remove from a.Bs at the same time, or we will get the old "Collection modified during enumeration" error

顺便说一句,我们首先实现的原因是因为我们不能同时枚举和删除a.Bs,或者我们将得到旧的“枚举期间修改的集合”错误

#1


1  

Clear() doesn't actually have any effect on the object from the perspective of the context, it just clears the in-memory collection of any existing elements; instead you would do something like this:

从上下文的角度来看,Clear()实际上对对象没有任何影响,它只是清除任何现有元素的内存中集合;相反,你会做这样的事情:

var a = db.As.Find(id);
var removals = a.Bs.ToList(); //or you could filter to only remove B objects matching a specific criteria, etc.
foreach (var remove in removals)
{
   a.Bs.Remove(remove);
}
db.SaveChanges();

This materialises all of Bs in a and then removes each one from the a.Bs collection - then when you SaveChanges the objects will be deleted from the database.

这将实现a中的所有B,然后从a.Bs集合中删除每个B - 然后当您SaveChanges时,将从数据库中删除对象。

Incidentally, the reason we materialise first is because we can't both enumerate and remove from a.Bs at the same time, or we will get the old "Collection modified during enumeration" error

顺便说一句,我们首先实现的原因是因为我们不能同时枚举和删除a.Bs,或者我们将得到旧的“枚举期间修改的集合”错误