实体框架集合已修改; 枚举操作可能无法执行

use*_*160 5 .net c# database asp.net entity-framework

我有一个问题,错误集的解决方案被修改,枚举操作可能无法执行.当"作者"和"z"表示相同的元素时,就会发生这种情况.

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




        }
    }


}

}
Run Code Online (Sandbox Code Playgroud)

我在Entity Framework 4.1和5上测试了代码

Sla*_*uma 5

如果您ally1在创建上下文后立即将它添加到上下文中,它将起作用:

//...
NationAlly ally1 = new NationAlly()
{
    Natio = author
};
context.NationAllys.Add(ally1);
//...
Run Code Online (Sandbox Code Playgroud)

问题与您特殊情况下的循环引用有关...

z-> z.NationAllys包含ally1-> ally1引用作者= z

...可能与此有关:

EF 4.1和“集合已被修改;枚举操作可能无法执行”。例外

我无法真正解释它,但是对我来说,这似乎是EF错误,因为您的代码应该可以正常工作。