2012-09-03 3 views
5

J'ai un problème avec la solution de l'erreur Collection a été modifiée, l'opération d'énumération peut ne pas s'exécuter. Cela se produit lorsque "author" et "z" suggèrent le même élément.Entity framework La collection a été modifiée; l'opération d'énumération peut ne pas s'exécuter

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




     } 
    } 


} 

} 

J'ai testé le code sur Entity Framework 4.1 et 5

Répondre

4

Il fonctionne si vous ajoutez le ally1 au contexte immedately une fois que vous avez créé:

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

Le problème doit faites avec la référence circulaire que vous avez dans votre cas particulier ...

z -> z.NationAllys contient ally1 -> ally1 se réfère à l'auteur = Z

... et est probablement lié à celui-ci:

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

Je ne peux pas vraiment l'expliquer, mais il ressemble à un bug EF pour moi que votre code devrait fonctionner sans problèmes.

+0

Oui! Je vous remercie. Mon problème était aussi les types autoréférentiels, où j'essayais d'indiquer à EF qu'il y avait une référence explicite dans les deux sens. Après avoir supprimé la référence enfant-> parent, cela a bien fonctionné! –

-1

Le problème est lié à la référence circulaire que vous avez. Vous devriez assigner zéro à une référence pour éviter les cycles

Questions connexes