0

I have a table in the database called Course_Predecessor and I'm trying to delete all the items from this table using EF and then adding new data to the table. I can't delete the data because I get the following error : "collection was modified enumeration operation may not execute"

this is the code I'm using (inside of a fucntion which recieves the context of the db as ctx)

List<Course_Predecessor> lst = new List<Course_Predecessor>();
fillTheList(ref lst , someData);

ctx.Course_Predecessor.RemoveRange(ctx.Course_Predecessor.ToList());
ctx.Course_Predecessor.AddRange(predecessors);

I get the error at the RemoveRange function.

would appreciate any help.

1
  • What happens if you do ctx.Course_Predecessor.RemoveRange(ctx.Course_Predecessor.ToList()); without any surrounding code? This should work fine. Commented Sep 27, 2016 at 20:41

3 Answers 3

1

You can try as following:

ctx.Course_Predecessor.ToList().ForEach(c => ctx.Course_Predecessor.Remove(c));
Sign up to request clarification or add additional context in comments.

2 Comments

it says the ForEach is not defined
Sorry, I missed to add .ToList(). See my updated answer. This should work for you.
1

If you are using EF6 (which is looks like you are) then you should be able to delete all records in the table by doing the following. (Note the absence of the .ToList() method in the RemoveRange call)

ctx.Course_Predecessor.RemoveRange(ctx.Course_Predecessor);
ctx.SaveChanges();

Alternatively you could do:

foreach (var predecessor in ctx.Course_Predecessor)
{
    ctx.Entry(predecessor).State = EntryState.Deleted;
}
ctx.SaveChanges();

Both sections of code do the exact same thing.

2 Comments

it still says the collection was modified
@Adam I don't think the RemoveRange call is triggering the collection modified exception. I'm able to run that exact code without any issues. It seems like you may be modifying a collection while enumerating it in a previous section of the code where you are using the same data context. Would it be possible for you to update your question to include the code for the fillTheList method and any other code that executes before you try to remove all the records from that table and then call SaveChanges?
0

Try this,

 ctx.Course_Predecessor.ToList().ForEach(r => ctx.Course_Predecessor.DeleteObject(r));
 ctx.SaveChanges();

6 Comments

I'm not modifying the elements. lst is a list of the new items I want to insert to the table (the function just inserts the values in the list) then I want to remove all the table and insert new values
@Adam did you try as suggested
I can't try this because it does not fit my situation my friend I do need those lines in order to insert the new values
try the above now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.