Entity Framework on delete cascade

You can use RemoveRange :

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

But this is for EF 6.0


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

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.


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

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

Comments

  1. Jesus

    • 2018/11/20

    Cascade delete automatically deletes dependent records or sets null to ForeignKey columns when the parent record is deleted in the database.

  2. Palmieri

    • 2018/7/16

    Cascade delete in the database. Many database systems also offer cascading behaviors that are triggered when an entity is deleted in the database. EF Core configures these behaviors based on the cascade delete behavior in the EF Core model when a database is created using EnsureCreated or EF Core migrations. For example, using the model above, the following table is created for posts when using SQL Server:

  3. Franco

    • 2017/7/27

    Cascade delete allows the deletion of a row to trigger the deletion of related rows automatically. EF will delete a child record(s) (database rows) 

  4. Brown

    • 2018/12/12

    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:

  5. Brian

    • 2021/1/10

    When the Delete() method in the UserRepository class is called, it does not delete the User record in the database because the foreign key in 

  6. Cameron

    • 2017/9/8

    Entity Framework Cascade Delete. Cascade Delete. Cascade delete allows the deletion of a row to trigger the deletion of related rows automatically. EF will delete a child record (s) (database rows) automatically when its parent is explicitly deleted via the DbContext.

  7. Felipe

    • 2018/11/21

    Cascade delete allows the deletion of a row to trigger the deletion of related rows automatically. EF Core covers a closely related concept and implements 

  8. Yousef

    • 2019/6/18

    Cascade delete allows the deletion of a row to trigger the deletion of related rows automatically. EF Core covers a closely related concept and implements several different delete behaviors and allows for the configuration of the delete behaviors of individual relationships. In Entity Framework Core, the OnDelete Fluent API method is used to specify the delete behavior for a dependent entity when the principal is deleted.

  9. Ivan

    • 2021/7/5

    c# - I’m using EF Core 3.1.1, but I believe this question applies to all versions of EF. It seems the EF has the ability to cascade delete 

  10. Messiah

    • 2020/3/27

    Entity Framework Cascade Delete with Multiple Column Key. 578. No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient' 2.

  11. Joaquin

    • 2020/8/29

    Entity Framework Core Cascade Delete is one of the Referential actions. These actions specify what to do with the related rows when we delete the parent row 

  12. Lefebvre

    • 2019/7/8

    Cascade - dependents should be deleted; Restrict - dependents are unaffected; SetNull - the foreign key values in dependent rows should update to NULL.

  13. Branson

    • 2015/10/24

    In Entity Framework Core, the OnDelete Fluent API method is used to specify the delete behavior for a dependent entity when the principal is deleted.

  14. Gashi

    • 2021/3/27

    What is Cascade Delete in Entity Framework and SQL? Cascade delete means deleting all the dependent record when the primary record gets deleted. Let's 

Comments are closed.

Recent Posts