0

我已经阅读了许多与此错误相关的答案,我也得到了一些与我的问题相近的答案,但我无法追踪我在这里做错了什么

我有一个类通用存储库

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
}

我的存储库类是

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;
    }
}

我走错路了吗??

4

1 回答 1

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;
    }
}

该设计存在抽象漏洞。对存储库使用构造函数注入来注入上下文。您可以在一定程度上防止意外创建多个上下文。使用依赖注入框架并使依赖项显式化,而无需在方法内实例化它们。

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;
    }

}
于 2012-04-23T06:51:01.530 回答