Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7849741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:37:24+00:00 2026-06-02T18:37:24+00:00

This has been driving me nuts for the last 2 days. I have 3

  • 0

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 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;
    }
}

and

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;
    }
}

Seems pretty simple. Not doing any specific configuration for those classes in DbContext.

But, when I run following code

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.
}

It throws following exception:

Collection was modified; enumeration operation may not execute.

Stack trace:

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)
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)

Anyone has an idea what’s going on and maybe how to fix it ?
Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:37:25+00:00Added an answer on June 2, 2026 at 6:37 pm

    I had the same problem. It looks like a bug in EF.

    I changed my code to explicitly add the entity to EF context before setting it to any other entity property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been driving me nuts for a week now. I have a class
This has been driving me nuts all day. I have a weird bug that
This has been driving me nuts for a few days. I am desperate for
This has been driving me absolutely nuts. I have a substitute function like this:
This one has been driving me nuts. I have a div that has it's
This one has been driving me nuts for a few days now and I've
This error has been driving me nuts. We have a server running Apache and
So this has just been driving me nuts! I have looked through tons of
I have a javascript function that has been driving me nuts. This is the
This has been driving me nuts for the past hour. I have two computers,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.