【问题标题】:Entity framework Collection was modified; enumeration operation may not execute实体框架集合被修改;枚举操作可能无法执行
【发布时间】:2012-09-03 15:47:14
【问题描述】:

我遇到了错误Collection was modified的解决方法,枚举操作可能无法执行。 当“author”和“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();




        }
    }


}

}

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

【问题讨论】:

    标签: c# asp.net .net database entity-framework


    【解决方案1】:

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

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

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

    z -> z.NationAllys 包含 ally1 -> ally1 指代作者 = z

    ...并且可能与这个有关:

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

    我无法真正解释它,但在我看来它是一个 EF 错误,因为您的代码应该可以毫无问题地运行。

    【讨论】:

    • 是的!谢谢你。我的问题也是自引用类型,我试图向 EF 表明两种方式都有明确的引用。删除 child->parent 引用后,它工作正常!
    【解决方案2】:

    问题与您拥有的循环引用有关。您应该为某些引用分配零以避免周期性

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多