Entity Framework on delete cascade

前端 未结 5 885
天涯浪人
天涯浪人 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 03:46

    You are not deleting the BookFormats from the database, but you are removing the relationship, thus orpahning your BookFormats and setting the BookID column to NULL. The delete cascade you have put on the database says When I delete theBook, then delete all of theBookFormatsthat have aBookIDequal to mine. You are not deleting the book you are deleting the formats from the Book.

    Instead of originalBook.BookFormats.Clear() you should have something like this...

    List<int> idsToDelete = new List<int>();
    
    foreach (BookFormat bf in originalBook.BookFormats)
    {
        idsToDelete.Add(bf.ID);
    }
    
    foreach (int id in idsToDelete)
    {
        BookFormat format = m.db.BookFormat.FirstOrDefault(x => x.ID == id);
        if (format != null)
        {
             m.db.DeleteBookFormat(format);
        }
    }
    
    m.db.SaveChanges();
    

    It should be something along those lines. I don't have it right in front of me to remember how EF constructs the delete method in the EDMX.

    0 讨论(0)
  • 2020-12-10 03:51

    I've tested it in EF 6.1.3 and this should work fine:

     var originalBook = m.db.Book.First(x => x.BookID == bookId);
     originalBook.BookFormats.Clear();
     db.Books.Remove(originalBook);
     m.db.SaveChanges();
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-10 04:03

    I use EF6 and this works.

            var itemBinding = db.ItemBinding.Where(x => x.BindingToId == id) ;
            foreach (var ib in itemBinding)
            {
                db.Item.Remove(ib.Item);
                db.ItemBinding.Remove(ib);
            }
            db.SaveChanges();
    
    0 讨论(0)
  • 2020-12-10 04:09

    You can use RemoveRange :

    m.db.BookFormats.RemoveRange(originalBook.BookFormats);
    m.db.SaveChanges();
    

    But this is for EF 6.0

    0 讨论(0)
提交回复
热议问题