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

  • SEARCH
  • Home
  • 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 8486933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:05:28+00:00 2026-06-10T21:05:28+00:00

I have a problem with the solution of the error Collection was modified, enumeration

  • 0

I have a problem with the solution of the error Collection was modified, enumeration operation may not execute.
It occurs when “author” and “z” suggests the same element.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace ConsoleApplication1
{
public class Nation
{
    public int ID { get; set; }
    public int name { get; set; }
    public virtual ICollection<NationAlly> NationAllys { get; set; }
}

public class NationAlly
{
    public int ID { get; set; }
    public int level { get; set; }
    public Nation Natio { get; set; }
}

public class NationsContext : DbContext
{
    public DbSet<Nation> Nations { get; set; }
    public DbSet<NationAlly> NationAllys { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Nation>()
            .HasMany(n => n.NationAllys)
            .WithRequired()
            .Map(conf => conf.MapKey("OwnerID"))
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<NationAlly>()
            .HasRequired(a => a.Natio)
            .WithMany()
            .Map(conf => conf.MapKey("UserID"))
            .WillCascadeOnDelete(false);
    }
}


class Program
{



    static void Main(string[] args)
    {
        using (var context = new NationsContext())
        {

            // We have three Nations and two Allies
            Nation nation1 = new Nation()
            {
                name = 1
            };
            Nation nation2 = new Nation()
            {
                name = 2
            };
            Nation nation3 = new Nation()
            {
                name = 3
            };



            context.Nations.Add(nation1);
            context.Nations.Add(nation2);
            context.Nations.Add(nation3);

            context.SaveChanges();

        }



        using (var context = new NationsContext())
        {
            Nation z = (from x in context.Nations
                      where x.name == 1
                        select x).FirstOrDefault();


            Nation author = (from x in context.Nations
                             where x.name == 1
                           select x).ToList().FirstOrDefault();

            NationAlly ally1 = new NationAlly()
            {
                Natio = author
            };

            // toNation of ally1 refers to Nation2
           // ally1.User = author;


            if (z.NationAllys != null)
            {
                z.NationAllys.Add(ally1);
            }
            else
            {
                z.NationAllys = new List<NationAlly>();
                z.NationAllys.Add(ally1);
            }




            context.SaveChanges();




        }
    }


}

}

I tested the code on Entity Framework 4.1 and 5

  • 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-10T21:05:30+00:00Added an answer on June 10, 2026 at 9:05 pm

    It works if you add the ally1 to the context immedately after you’ve created it:

    //...
    NationAlly ally1 = new NationAlly()
    {
        Natio = author
    };
    context.NationAllys.Add(ally1);
    //...
    

    The problem has to do with the circular reference you have in your special case…

    z -> z.NationAllys contains ally1 -> ally1 refers to author = z

    …and is likely related to this one:

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

    I can’t really explain it, but it looks like an EF bug to me as your code should work without problems.

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

Sidebar

Related Questions

i have this problem: ms-access could not delete and i found a potential solution
I have problem with copying sdf file to WM emulator. My solution structure MyApp.DataLayer
I have a problem and can't find a solution. I am making an accordion
I have a problem and search for a solution on Google but can't find
My problem (and solution?) is simple - I hope :) I have a RIA
I have a problem which i can't seem to find the solution to. I
I have a problem with UISwipeGestureRecognizer and can't find the solution for it. I
I have this problem and i just cannot find a solution to it. I
I really have a problem, I have a VS 2010 solution and it suddenly
i have a little problem and see no solution for it. I need flexible

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.