2012-04-23 13 views
0

このエラーに関する多くの回答を読んでいますが、私の質問に近いいくつかの回答がありますが、 doinのMここで間違っエンティティフレームワーク4.1エンティティオブジェクトはIEntityChangeTrackerの複数のインスタンスで参照できません

を私は

public abstract class GenericRepository<TC,T>:IGenericRepository<T> where T:class where TC:ObjectContext , new() 
{ 
    private TC _entities = new TC(); 
    public TC Context 
    { 

     get { return _entities; } 
     set { _entities = value; } 
    } 
    public virtual IQueryable<T> GetAll() 
    { 

     IQueryable<T> query = _entities.CreateObjectSet<T>(); 
     return query; 
    } 
    public virtual void Add(T entity) 
    { 
     _entities.CreateObjectSet<T>().AddObject(entity); 
    } 
    // save,update,insert etc etc 
} 

クラスの一般的なリポジトリを持っており、私のリポジトリクラスiが

public class MenuRepository:GenericRepository<mbsEntities,menu>,IMenu 
{ 
    public menu GetMenu(int id) 
    { 
     return GetAll().FirstOrDefault(x => x.menu_id == id); 
    } 
    public bool CreateMenu(string menuName, int menuLevel, string menuUrl, int menuParent,int menuPosition,int roleId) 
    { 
     if(!Profile.IsInRole(Enums.Enumerations.Roles.Admin)) 
      return false; 
     var menu = new menu() 
         { 
          menu_name = menuName, 
          menu_level=menuLevel, 
          menu_url = menuUrl, 
          menu_parent = menuParent, 
          menu_position = menuPosition, 

         }; 
     var roleRepository = new RoleRepository(); 
     var role = roleRepository.GetAll().FirstOrDefault(x => x.id == roleId); 
     menu.traffic_role.Add(role); 
     try 
     { 
      Add(menu);  // here im getting error “An entity object cannot be referenced by multiple instances of IEntityChangeTracker” 
      Save(); 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
     return true; 
    } 
} 

をやっていている間違いましたで方法?

答えて

1

あなたの「MenuRepository」とRoleRepositoryリポジトリは異なるコンテキストを使用します。クエリーを実行する前にRoleRepositoryのコンテキストをMenuRepositoryのコンテキストを使用するように設定します。

public class MenuRepository:GenericRepository<mbsEntities,menu>,IMenu 
{ 
    public menu GetMenu(int id) 
    { 
     return GetAll().FirstOrDefault(x => x.menu_id == id); 
    } 
    public bool CreateMenu(string menuName, int menuLevel, string menuUrl, int menuParent,int menuPosition,int roleId) 
    { 
     if(!Profile.IsInRole(Enums.Enumerations.Roles.Admin)) 
      return false; 
     var menu = new menu() 
         { 
          menu_name = menuName, 
          menu_level=menuLevel, 
          menu_url = menuUrl, 
          menu_parent = menuParent, 
          menu_position = menuPosition, 

         }; 
     var roleRepository = new RoleRepository(); 

     roleRepository.Context = Context; 

     var role = roleRepository.GetAll().FirstOrDefault(x => x.id == roleId); 
     menu.traffic_role.Add(role); 
     try 
     { 
      Add(menu);  // here im getting error “An entity object cannot be referenced by multiple instances of IEntityChangeTracker” 
      Save(); 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
     return true; 
    } 
} 

デザインに漏れ込みがあります。リポジトリにコンストラクタインジェクションを使用してコンテキストを注入する。誤って複数のコンテキストをある程度作成することを防ぐことができます。 Dependency Injectionフレームワークを使用し、依存関係をメソッド内でインスタンス化せずに明示的にします。

public abstract class GenericRepository<TC,T>:IGenericRepository<T> where T:class where TC:ObjectContext , new() 
{ 
    protected GenericRepository(TC context) 
    { 
     Context = context; 
    } 

    public TC Context 
    { 
     get; protected set; 
    } 

} 
+0

ohh !!魅力的なように働いた...私はその部分を逃した、ありがとうトン – FosterZ

関連する問題