gpt4 book ai didi

c# - EF 4.1 和 "Collection was modified; enumeration operation may not execute."异常

转载 作者:可可西里 更新时间:2023-11-01 07:45:03 25 4
gpt4 key购买 nike

在过去的 2 天里,这让我抓狂。我有 3 个非常基本的类(好吧,为了便于阅读而减少了)

public class Employee 
{
public string Name { set; get; }
virtual public Employer Employer { set; get; }

public Employee(string name)
{
this.Name = name;
}
}

,

// this basically ties Employee and his role in a company.
public class EmployeeRole{
public int Id { set; get; }
virtual public Employee Employee { set; get; }
public string Role { set; get; }

public EmployeeRole(Employee employee, string role){
this.Employee = employee;
this.Role = role;
}
}

public class Employer{

public string Name { set; get; }
List<EmployeeRole> employees = new List<EmployeeRole>();
virtual public List<EmployeeRole> Employees { get { return this.employees; } }

public Employer(string name, Employee creator){
this.Name = name;
this.Employees.Add(new EmployeeRole(creator, "Creator"));
creator.Employer = this;
}
}

看起来很简单。不对 DbContext 中的这些类进行任何特定配置。

但是,当我运行以下代码时

using (DbContext db = DbContext.GetNewDbContext()){

Employee creator = new Employee("Bob");
db.Employees.Add(creator);
db.SaveChanges();

Employer employer = new Employer("employer", creator);
db.Employers.Add(employer);
db.SaveChanges();
// I know I can call SaveChanges once (and it actually works in this case),
// but I want to make sure this would work with saved entities.
}

它抛出以下异常:

Collection was modified; enumeration operation may not execute.

堆栈跟踪:

at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List1.Enumerator.MoveNextRare() at
System.Collections.Generic.List
1.Enumerator.MoveNext() at System.Data.Objects.ObjectStateManager.PerformAdd(IList1 entries)<br/>
at
System.Data.Objects.ObjectStateManager.AlignChangesInRelationships(IList
1 entries) at System.Data.Objects.ObjectStateManager.DetectChanges() at System.Data.Objects.ObjectContext.DetectChanges() at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force) at System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action,
EntityState newState, Object entity, String methodName) at
System.Data.Entity.Internal.Linq.InternalSet
1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)

任何人都知道发生了什么,也许如何解决它?谢谢!

最佳答案

对我来说,这看起来像是 Entity Framework 中的一个错误。我已经将您的示例简化为一个更简单但具有相同结构的示例:

public class TestA // corresponds to your Employee
{
public int Id { get; set; }
public TestB TestB { get; set; } // your Employer
}

public class TestB // your Employer
{
public TestB()
{
TestCs = new List<TestC>();
}

public int Id { get; set; }
public ICollection<TestC> TestCs { get; set; } // your EmployeeRoles
}

public class TestC // your EmployeeRole
{
public int Id { get; set; }
public TestA TestA { get; set; } // your Employee
}

这是三个具有循环关系的实体:

TestA -> TestB -> TestC -> TestA

如果我现在使用与您的结构相同的相应代码,我会得到相同的异常:

var testA = new TestA();
var testB = new TestB();
var testC = new TestC();

context.TestAs.Add(testA);

testA.TestB = testB;
testB.TestCs.Add(testC);
testC.TestA = testA;

context.ChangeTracker.DetectChanges();

请注意,我使用了 DetectChanges而不是 SaveChanges因为异常中的堆栈跟踪清楚地表明实际上 DetectChanges导致异常(由 SaveChanges 内部调用)。我还发现调用 SaveChanges两次不是问题。这里的问题只是在整个对象图完成之前“提前”添加到上下文。

被修改的集合(因为异常正在提示)不是 TestB.TestCs模型中的集合。它似乎是 ObjectStateManager 中的条目集合.我可以通过替换 ICollection<TestC> TestCs 来验证这一点通过 TestC TestC 进行单一引用在TestB类(class)。这样,模型根本不包含任何集合,但它仍然会抛出关于修改后的集合的相同异常。 (SaveChanges 将失败,尽管有三个单一引用,因为 EF 不知道由于循环而以何种顺序保存实体。但这是另一个问题。)

我认为这是一个错误,EF 更改检测 (DetectChanges) 似乎修改了它自己的内部集合,它只是在迭代。

现在,解决这个问题很简单:只需 Add在调用 SaveChanges 之前的最后一步将实体添加到上下文中:

var testA = new TestA();
var testB = new TestB();
var testC = new TestC();

testA.TestB = testB;
testB.TestCs.Add(testC);
testC.TestA = testA;

context.TestAs.Add(testA);

context.ChangeTracker.DetectChanges();

EF 会将整个相关对象图添加到上下文中。此代码成功(也使用 SaveChanges 而不是 DetectChanges )。

或者你的例子:

using (DbContext db = DbContext.GetNewDbContext()){
Employee creator = new Employee("Bob");
Employer employer = new Employer("employer", creator);

db.Employees.Add(creator);
db.SaveChanges();
}

编辑

这是相同的异常:Entity Framework throws "Collection was modified" when using observable collection .按照该示例中的代码,情况类似:将实体添加到上下文,然后更改/添加与该实体的关系。

编辑2

有趣的是,这会引发相同的异常:

var testA = context.TestAs.Find(1); // assuming there is already one in the DB
var testB = new TestB();
var testC = new TestC();

testA.TestB = testB;
testB.TestCs.Add(testC);
testC.TestA = testA;

context.SaveChanges(); // or DetectChanges, it doesn't matter

因此,我想将与新实体的关系添加到现有实体。此问题的修复似乎不太明显。

关于c# - EF 4.1 和 "Collection was modified; enumeration operation may not execute."异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com