【发布时间】:2016-09-27 18:11:54
【问题描述】:
我在数据库中有一个名为 Course_Predecessor 的表,我正在尝试使用 EF 删除该表中的所有项目,然后将新数据添加到表中。 我无法删除数据,因为我收到以下错误:“collection was modified enumeration operation may not execute”
这是我正在使用的代码(在将 db 的上下文接收为 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);
我在 RemoveRange 函数中遇到错误。
将不胜感激。
【问题讨论】:
-
如果您在没有任何周围代码的情况下执行
ctx.Course_Predecessor.RemoveRange(ctx.Course_Predecessor.ToList());会发生什么?这应该可以正常工作。
标签: c# database entity-framework asp.net-mvc-4 entity-framework-6