Entity Framework on delete cascade

前端 未结 5 886
天涯浪人
天涯浪人 2020-12-10 03:31

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

5条回答
  •  [愿得一人]
    2020-12-10 04:00

    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();
    

提交回复
热议问题