I have problem with deleting related rows in Entity Framework 4.1. I have tables with relations
Book 1<--->* BookFormats
I have set the on delete cascade
Cascade deletions concept is as follows:
When you delete Book
from the DB all related BookFormats
will be deleted for you by SQL Server (please note that it doesn't matter how deletion of Book
will be initiated via EF or raw SQL). Thus it has nothing to do with your task: "I want to delete all BookFormats
related to my Book
". To accomplish it you need something like this:
foreach(var m in m.db.BookFormats.Where(f=>f.BookID == bookID))
{
m.db.BookFormats.Remove(m);
}
m.db.SaveChanges();