EF 4.1 and “Collection was modified; enumeration operation may not execute.” exception

前端 未结 4 1593
梦如初夏
梦如初夏 2020-12-09 10:27

This has been driving me nuts for the last 2 days. I have 3 pretty basic classes (well, reduced for readability)

public class Employee 
{
    public string N         


        
4条回答
  •  我在风中等你
    2020-12-09 11:11

    I ran into the same issue this morning, in my case I had an "Employee-Manager" association defined with a circular relationship. For example:

    public class Employee 
    {
        public string Name { set; get; }
        virtual public Employee Manager { set; get; }
    
        public Employee()
        {
    
        }
    }
    

    The app was crashing with the error above when setting the Manager property. At the end it turned out to be a bad implementation of the GetHashCode() method in the Employee class. It seemed that EF was not able to spot the modified entity since the comparison among the entities was failing; thus, EF was thinking the collection had been modified.

提交回复
热议问题