实体框架集合已修改;枚举操作可能不执行 [英] Entity framework Collection was modified; enumeration operation may not execute

查看:362
本文介绍了实体框架集合已修改;枚举操作可能不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合被修改了错误的解决方案的问题,枚举操作可能无法执行。 它发生时,作者和Z意味着相同的元素

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




        }
    }


}

}

我测试了code对实体框架4.1和5

I tested the code on Entity Framework 4.1 and 5

推荐答案

,如果你的 ally1 添加到你创建它immedately后的背景如下:

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.NationAllys包含ally1 - > ally1是指作者= Z

...并很可能与这一个:

...and is likely related to this one:

<一个href="http://stackoverflow.com/questions/7536459/ef-4-1-and-collection-was-modified-enumeration-operation-may-not-execute-exc">EF 4.1和&QUOT;集合已修改;枚举操作可能不会执行与QUOT。除了

我真的不能解释它,但它看起来像一个EF臭虫把我当成你的code就没有问题了。

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

这篇关于实体框架集合已修改;枚举操作可能不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆