Entity framework Code First with Migrations Cascade Delete is not working

c# entity-framework-6

Question

I have a Code First Model and I'm trying to delete all rows which was referenced to a certain ID, the problem is, it was working before I use Migrations. this is my Code :

var query = context.user.Include(p => p.area).
                    Include(p => p.city).Include(p => p.city.state).
                    Include(p => p.city.state.country).
                    Include(p => p.favorites).Include(p => p.ads).FirstOrDefault(a => a.id_user == result);

                context.user.Remove(query);
                context.Entry(query).State = EntityState.Deleted;
                context.SaveChanges();

It just delete the user table, not all!

1
0
12/8/2016 11:14:49 PM

Popular Answer

Either turn cascade delete on for user so when it is deleted, all its children will be deleted as well. Like this:

modelBuilder.Entity<user>()
            .WillCascadeOnDelete(true); 

If you do not want that, you can use RemoveRange like this:

context.user.RemoveRange(query);

Or you can loop through each child and delete them individually like this:

foreach (var record in context.user.area)
{
    user.area.Remove(record);
}

And last thing, you can remove one of your includes since you do not need it.

var query = context.user.Include(p => p.area).
                    Include(p => p.city.state.country).
                    Include(p => p.favorites).Include(p => p.ads).FirstOrDefault(a => a.id_user == result);

EDIT 1

I am not sure what kind of relationships you have setup between your entities. WillCascadeOnDelete(true) should delete the children when the parent is deleted. Here is an example:

modelBuilder.Entity<Course>() 
    .HasRequired(t => t.Department) 
    .WithMany(t => t.Courses) 
    .HasForeignKey(d => d.DepartmentID) 
    .WillCascadeOnDelete(true);

What the above is saying is this: Course must have a Department, and a Department has many courses, and a Course has a foreign-key named DepartmentID. When a Department is deleted, cascade the deletion: meaning delete all the courses (children) of this department. Here the relationship is one department can have many courses: one to many.

If a child object has a non-nullable foreign-key then EF will delete the child when the parent is deleted: cascade on delete will be set by default. If the child can have a nullable foreign key, then the child will not be deleted when the parent is deleted.

So from the above we can tell that the deletion strategy EF uses depends on how you have setup your model.

Your query returns one user. Therefore, if you want to delete all the children, they will be deleted automatically if you have built your model that way.

If you have not, then delete the children one by one. If a user has one child of area then do this:

context.user.area.Remove(query.user.area);

If a user has many area then use the foreach strategy I have shown above.

0
12/9/2016 2:40:05 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow