Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF - Cascade Delete Not Working, Can't Delete Object

I get this error:

System.Data.SqlClient.SqlException The DELETE statement conflicted with the REFERENCE constraint "FK_comments_postId__164452B1". The conflict occurred in database "awe", table "dbo.comments", column 'postId'. The statement has been terminated.

I have this structure:

    public class Post
    {
        public long Id { get; set; }
        public string Body { get; set; }     

        public long? ParentId { get; set; }
        public virtual Post Parent { get; set; }
        public virtual ICollection<Post> Posts { get; set; }

        public virtual ICollection<Comment> Comments { get; set; }
    }

    public class Comment
    {
        public long Id { get; set; }
        public long PostId { get; set; }
        public virtual Post Post { get; set; }
        public string Body { get; set; }
    }

my delete method:

    public void Delete(long id)
    {
        var p = context.Set<Post>().Get(id);
        if(p == null) throw new MyEx("this post doesn't exist");
        if (p.Posts.Count > 0) throw new MyEx("this post has children and it cannot be  deleted");
        context.Set<Post>().Remove(p);
        context.SaveChanges();
    }

my DbContext:

public class Db : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<Comment> Comments { get; set; }
}
like image 553
Omu Avatar asked Sep 26 '11 13:09

Omu


People also ask

How do I enable cascade delete in Entity Framework?

Cascade delete automatically deletes dependent records or sets null to ForeignKey columns when the parent record is deleted in the database. Cascade delete is enabled by default in Entity Framework for all types of relationships such as one-to-one, one-to-many and many-to-many.

How do I delete on delete cascade?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.

How do I enable cascade delete in SQL Server?

Here's the way I would add the "cascading delete" feature to an existing foreign key in SQL Server Management Studio. First, find your foreign key, and open it's "DROP and CREATE To" in a new Query window. Then just hit hit the "Execute" button to run the query. Job done !

How do I remove a one to many relationship in Entity Framework?

after manual set the property,single call to "dbContext. As. Remove(someA)" work as expected!


1 Answers

It sounds like the Post that you are trying to delete has child Comments.

Entity Framework will not take responsibility for cascading a delete in the database – it expects that you will achieve this by setting a cascading delete on the foreign key relationship in the RDBMS.

Having said this, if you delete a parent entity in Entity Framework, it will attempt to issue delete statements for any child entities which have been loaded into the current DbContext, but it will not initialize any child entities which have not yet been loaded. This may lead to the RDBMS throwing foreign key constraint violation exceptions if a cascading delete has not been specified, like the one you are seeing. For more details about how cascade delete “works” in Entity Framework, see this blog post.

like image 67
Ian Nelson Avatar answered Oct 29 '22 20:10

Ian Nelson