开发者

EF: Deleting from Many-to-Many (Bridge) table

I've got a setup similar to this:

Table1 
    id1 (int)
    name (varchar)

Table 2
    id2 (int)
    name (varchar)

Bridge
    id1 (fk to Table1)
    id2 (fk to Table2)

As you all know, in the EF, an object won't exist to represent Bridge. Instead, Table1 will contain a collection of Table2's and Table2 will contain a collection of Table1's.

Let's say I have a single Table1 record associated with 5 Table2's.

How do I delete all Table2 references efficiently? I only wan开发者_开发知识库t them deleted from the Bridge table...


I think you just clear the references and then save the changes - I don't think it deletes the Table2 records, but I may be wrong:

var query = from item in context.Table1
            where item.id1 == id1
            select item;

var table1 = query.Single();
table1.Table2s.Clear();

context.SaveChanges();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜