Having the 2 entities: A *<-->* B
many-to-many relationship and corresponding 3 tables: A, B, AB
.
Trying the following code:
var tempA = this.dbContext.A.
.Where(a => a.UID == 1)
.FirstOrDefault();
// {check for null here}
tempA.B.Clear();
this.dbContext.SaveChanges();
seems to generate multiple 'Delete' sql calls to database, for-each b
in tempA.B
collection (somewhat strange, unless I miss something).
So, if I have AB table with these 2 records = { (1,2) , (1,3) } , the above code produces two 'Delete' sql calls (one for b=2 & another for b=3), something like this:
1. DELETE FROM AB WHERE (A_UID = 1) AND (B_UID = 2)
and
2. DELETE FROM AB WHERE (A_UID = 1) AND (B_UID = 3)
But I want some EF code which could produce something simple as the below?
DELETE FROM AB WHERE (A_UID = 1)
EDIT: I don't understand why EF is parsing all the inner references and generates one delete for-each of them, instead one delete for all of them.