尝试将EF函数与通用IRepository模式一起使用时出错

时间:2017-06-19 18:13:57

标签: c# asp.net-mvc entity-framework design-patterns irepository

我已经在我的应用程序中实现了通用存储库模式,使用统一容器作为DI提供程序,但是当我开始测试它是否有效时,会出现此错误" IEntityChangeTracker"

的多个实例不能引用实体对象

我的存储库代码

public interface IRepository<T> where T : class
    {
        IEnumerable<T> GetAll();
        T Get(int id);
        T Add(T item);
        bool Update(T item);
        bool Delete(T item);
        IEnumerable<T> Find(Func<T, bool> predicate);
        void Save();
    }

    public class Repository<M, T> : IRepository<T> where T : class where M : DbContext, new()
    {
        private ILogger _Log;

        public Repository(ILogger log)
        {
            _Log = log;
        }

        private M _entities = new M();
        public M Context
        {

            get { return _entities; }
            set { _entities = value; }
        }

        public T Add(T item)
        {
            try
            {
                _entities.Set<T>().Add(item);
                return null;
            }
            catch (Exception ex)
            {
                _Log.LogErorr(this.GetType().Name + "Add Method", ex.Message + "--" + ex.InnerException);
                throw;
            }
        }

        public virtual bool Delete(T item)
        {
            try
            {
                _entities.Set<T>().Remove(item);
                return true;
            }
            catch (Exception ex)
            {
                _Log.LogErorr(this.GetType().Name + "Delete Method", ex.Message + "--" + ex.InnerException);
                throw;
            }

        }

        public IEnumerable<T> Find(Func<T, bool> predicate)
        {
            IEnumerable<T> query = _entities.Set<T>().Where(predicate);
            return query;
        }

        public T Get(int id)
        {
            return _entities.Set<T>().FirstOrDefault();
        }

        public IEnumerable<T> GetAll()
        {
            IEnumerable<T> data = _entities.Set<T>();
            return data;
        }

        public bool Update(T item)
        {
            try
            {
                _entities.Entry(item).State = EntityState.Modified;
                return true;
            }
            catch (Exception ex)
            {

                _Log.LogErorr(this.GetType().Name + "Update Method", ex.Message + "--" + ex.InnerException);
                throw;
            }
        }

        public virtual void Save()
        {
            try
            {
                _entities.SaveChanges();
            }
            catch (Exception ex)
            {
                _Log.LogErorr(this.GetType().Name + "Save Method", ex.Message + "--" + ex.InnerException);
                throw;
            }
        }
    }
}

然后我尝试通过此代码向DB添加一些虚拟数据,并向控制器注入服务

public class HomeController : Controller
    {
 private IRepository<account> _AccountRepository;
        public HomeController(ILogger Log,
                              IRepository<account> AccountRepository)
        {
            _Log = Log;
            _AccountRepository = AccountRepository;
        }
account ac = new account()
 _AccountRepository.Add(ac);
_AccountRepository.Save();

}

0 个答案:

没有答案