Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 实体框架4.1一个实体对象不能被IEntityChangeTracker的多个实例引用_Asp.net Mvc 3_Entity Framework 4.1 - Fatal编程技术网

Asp.net mvc 3 实体框架4.1一个实体对象不能被IEntityChangeTracker的多个实例引用

Asp.net mvc 3 实体框架4.1一个实体对象不能被IEntityChangeTracker的多个实例引用,asp.net-mvc-3,entity-framework-4.1,Asp.net Mvc 3,Entity Framework 4.1,我已经阅读了很多关于这个错误的答案,我也得到了一些接近我的问题的答案,但我无法追踪我在这里做错了什么 我有一个类通用存储库 public abstract class GenericRepository<TC,T>:IGenericRepository<T> where T:class where TC:ObjectContext , new() { private TC _entities = new TC(); public TC Context

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

我有一个类通用存储库

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抽象类GenericRepository:IGenericRepository其中T:class其中TC:ObjectContext,new()
{
私有TC_实体=新TC();
公共TC环境
{
获取{return\u entities;}
设置{u entities=value;}
}
公共虚拟IQueryable GetAll()
{
IQueryable查询=_entities.CreateObjectSet();
返回查询;
}
公共虚拟空添加(T实体)
{
_entities.CreateObjectSet().AddObject(实体);
}
//保存、更新、插入等
}
我的存储库类是

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;
    }
}
公共类菜单存储:通用存储,IMenu
{
公共菜单GetMenu(内部id)
{
返回GetAll().FirstOrDefault(x=>x.menu\u id==id);
}
public bool CreateMenu(string menuName、int menuLevel、string menuUrl、int menuParent、int menupposition、int roleId)
{
if(!Profile.IsInRole(Enums.Enumerations.Roles.Admin))
返回false;
变量菜单=新菜单()
{
菜单名=菜单名,
菜单级别=菜单级别,
菜单url=menuUrl,
菜单\父菜单=菜单父菜单,
菜单位置=菜单位置,
};
var roleRepository=新roleRepository();
var role=roleRepository.GetAll().FirstOrDefault(x=>x.id==roleId);
菜单.流量\角色.添加(角色);
尝试
{
添加(菜单);//此处出现错误“一个实体对象不能被多个IEntityChangeTracker实例引用”
Save();
}
捕获(例外)
{
返回false;
}
返回true;
}
}

我走错了吗?

您的“MenuRepository”和
角色pository
存储库使用不同的上下文。设置
rolereposition
的上下文,以便在查询之前使用
menureposition
的上下文

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;
    }
}
公共类菜单存储:通用存储,IMenu
{
公共菜单GetMenu(内部id)
{
返回GetAll().FirstOrDefault(x=>x.menu\u id==id);
}
public bool CreateMenu(string menuName、int menuLevel、string menuUrl、int menuParent、int menupposition、int roleId)
{
if(!Profile.IsInRole(Enums.Enumerations.Roles.Admin))
返回false;
变量菜单=新菜单()
{
菜单名=菜单名,
菜单级别=菜单级别,
菜单url=menuUrl,
菜单\父菜单=菜单父菜单,
菜单位置=菜单位置,
};
var roleRepository=新roleRepository();
roleRepository.Context=上下文;
var role=roleRepository.GetAll().FirstOrDefault(x=>x.id==roleId);
菜单.流量\角色.添加(角色);
尝试
{
添加(菜单);//此处出现错误“一个实体对象不能被多个IEntityChangeTracker实例引用”
Save();
}
捕获(例外)
{
返回false;
}
返回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;
    }

}
public抽象类GenericRepository:IGenericRepository其中T:class其中TC:ObjectContext,new()
{
受保护的GenericRepository(TC上下文)
{
上下文=上下文;
}
公共TC环境
{
获取;保护集;
}
}

ohh!!工作得很有魅力。。。我错过了那部分,非常感谢