5

I have 1:N relationship between Program and Student tables which EF converted into navigation property. Now I want to delete all those records in this navigation students. I started like this:

foreach(Student student in program.Students)
program.Students.Remove(student);

But I am a little bit skeptical about this.

Than I tried this way:

while (program.Students.Count > 0)
    program.Students.Remove(program.Students.ToList()[0]);

But this seems weird too.

Is there some simpler way to do this or if not which way is the best?

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
Vajda
  • 1,795
  • 6
  • 39
  • 49

3 Answers3

26

Unless you have very special association called identifying relation between your program and students you should use something like this:

foreach(var student in program.Students.ToList())
{
    program.Students.Remove(student); // Break realation
    context.Students.DeleteObject(student); // Delete student
}

Removing student from the navigation property will only set FK in the student to null but the record will not be deleted. If FK is not nullable you will get an exception.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 1
    And what about .Clear() method? – Vajda May 31 '11 at 09:07
  • 5
    Clear method should still only break relations but it will not delete students. – Ladislav Mrnka May 31 '11 at 09:11
  • I just checked my table data after calling Clear(), all data are deleted. So Clear() deletes all data. There might only happen if this data without relation hasn't shown when I look in table data, in which I hardly doubt. I missed to say that i call SaveChanges() after Clear(), Ladislav, maybe you thought about this without calling SaveChanges() method. – Vajda May 31 '11 at 09:15
  • How is your `Student` class defined? Are you using POCOs or EntityObjects? – Ladislav Mrnka May 31 '11 at 09:33
  • It is Entity object, I used my DB model to create EF model – Vajda May 31 '11 at 18:10
  • 1
    Old, but when using `Clear()` and `Remove()`, I get a foreign key violation. When I use `DeleteObject()`, I don't get any errors. – Justin May 21 '13 at 17:46
  • Vajda's statement is incorrect. All data are NOT deleted when calling Clear. If the foreign key property of items in the collection is non-nullable, it will be nulled and the entity will be invalid and an exception will be thrown on SaveChanges. You must actually delete the entity after removing it from the collection. – Triynko Jul 24 '17 at 15:34
  • I don't have the DeleteObject() method. I am using Asp.net MVC 5 and probably another version of EF. In my case, it was enough to just call the remove without anything else :) – eMi May 01 '20 at 00:49
13

I really don't know if it's gonna work or not, but I can not help it, I'm curious. Is program.Students.Clear() working? Or maybe reset it, reinitializing it? I hope it helped you...

Edit: Please use @Ladislav Mrnka's response, as that is the correct one. I have tried to remove the answer but I cannot because it's the accepted one

Adrian Ancuta
  • 1,036
  • 9
  • 16
  • Hehe :). I can't believe that I missed Clear() :) I was searching for something like RemoveAll, DeleteAll :) – Vajda May 30 '11 at 21:51
  • 6
    As Ladislav says, `Clear()` will only null out the FK. If you have a nullable type this will not throw an error, but the Student record will still be there. If the FK column is non-nullable then it will throw an error on `SaveChanges()`. I am currently using EF5/6 Code-First. – Despertar Oct 22 '13 at 19:48
  • 1
    When I removing with Clear() method. Got this error : The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. – Søren Mar 31 '14 at 11:04
  • This is not the answer. Using "clear" to clear the navigation property collection does NOT remove the entities, and results in the error described by Soren. – Triynko Jul 24 '17 at 15:32
  • Wrong answer all you do with Clear is remove the relation – Mark Homer Feb 06 '18 at 16:48
1

.Clear() method is very good. It deletes all navigation properties in database. That's simple rather than foreach loop.