更新使用存储库模式记录与实体框架6 [英] Updating records using a Repository Pattern with Entity Framework 6

查看:163
本文介绍了更新使用存储库模式记录与实体框架6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个简单的博客应用程序,并想在我的通用存储库模式建立CRUD操作,但我发现我的更新方法,指出了错误:


  

System.Data.Entity.DbSet'不包含一个定义
  '进入'和没有扩展方法'进入'接受的第一个参数
  类型'System.Data.Entity.DbSet'可以找到(是否缺少
  使用指令或程序集引用?)


我也跟着<一个href=\"http://stackoverflow.com/questions/5035323/mocking-or-faking-dbentityentry-or-creating-a-new-dbentityentry\">post这如何'假'输入()加入过的DbContext间接的附加级别的解释。然而,在MVC 5我们继承: IdentityDbContext 并没有的DbContext。我曾尝试推行作者修复,但错误依然存在。

我的问题

我如何使用IdentityDbContext实体框架6添加更新的方法来我的仓库?如果我们不应该做这样那我怎么用这个模式更新记录?

我要指出,其他所有其他方法正常工作。

我的通用存储库:

 公共类BlogEngineRepository&LT; T&GT; :IRepository&LT; T&GT;其中T:类
    {
        保护DbSet&LT; T&GT; DbSet;        公共BlogEngineRepository(的DbContext的DataContext)
        {
            DbSet = dataContext.Set&LT; T&GT;();
        }        #地区IRepository&LT; T&GT;会员        公共无效插入(T实体)
        {
            DbSet.Add(实体);
        }        公共无效删除(T实体)
        {
            DbSet.Remove(实体);
        }        公共无效更新(T实体)
        {           DbSet.Entry(实体).STATE = System.Data.Entity.EntityState.Modified;        }        公众的IQueryable&LT; T&GT; SearchFor(前pression&LT;&Func键LT; T,BOOL&GT;&GT; predicate)
        {
            返回DbSet.Where(predicate);
        }        公众的IQueryable&LT; T&GT;得到所有()
        {
            返回DbSet;
        }        公共ŧGetById(INT ID)
        {
            返回DbSet.Find(ID);
        }        #endregion
    }


解决方案

好吧,我想通了这一点。之所以没有的更新的新库模式(实体框架6)是因为有没有需要一个。您只需拨打你的记录增长了ID,进行更改,然后提交/保存。

例如,这是从我PostController中我的编辑POST方法:

  [HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(假)]
公众的ActionResult编辑([绑定(包括=ID,标题,IntroText,身体,修改,作者)发布后)
{
    使用(的UnitOfWork uwork =新的UnitOfWork())
    {
        帖子编辑= uwork.PostRepository.GetById(post.Id);
        edit.Title = post.Title;
        edit.IntroText = post.IntroText;
        edit.Body = post.Body;
        edit.Modified = DateTime.Now;        uwork.Commit();        返回RedirectToAction(「指数」);
    }
}

RepositoryPattern看起来是这样的:

 公共类BlogEngineRepository&LT; T&GT; :IRepository&LT; T&GT;其中T:类
    {
        保护DbSet&LT; T&GT; DbSet;        公共BlogEngineRepository(的DbContext的DataContext)
        {
            DbSet = dataContext.Set&LT; T&GT;();
        }        #地区IRepository&LT; T&GT;会员        公共无效插入(T实体)
        {
            DbSet.Add(实体);
        }        公共无效删除(T实体)
        {
            DbSet.Remove(实体);
        }        公众的IQueryable&LT; T&GT; SearchFor(前pression&LT;&Func键LT; T,BOOL&GT;&GT; predicate)
        {
            返回DbSet.Where(predicate);
        }        公众的IQueryable&LT; T&GT;得到所有()
        {
            返回DbSet;
        }        公共ŧGetById(INT ID)
        {
            返回DbSet.Find(ID);
        }    }

I'm writing a simple blog application and trying to establish CRUD operations in my generic repository pattern but I'm getting an error on my update method that says:

'System.Data.Entity.DbSet' does not contain a definition for 'Entry' and no extension method 'Entry' accepting a first argument of type 'System.Data.Entity.DbSet' could be found (are you missing a using directive or an assembly reference?)

I followed a post that explained how to 'fake' Entry() by adding additional level of indirection over DbContext. However in MVC 5 we're inheriting from: IdentityDbContext and not DbContext. I did try implementing the authors fix but the error persists.

My Question

How can I add an update method to my repository in Entity Framework 6 using IdentityDbContext? If we aren't supposed to do it this way then how do I update a record with this pattern?

I should note that all other the other methods work as expected.

My generic Repository:

public class BlogEngineRepository<T> : IRepository<T> where T : class
    {
        protected DbSet<T> DbSet;

        public BlogEngineRepository(DbContext dataContext)
        {
            DbSet = dataContext.Set<T>();
        }

        #region IRepository<T> Members

        public void Insert(T entity)
        {
            DbSet.Add(entity);
        }

        public void Delete(T entity)
        {
            DbSet.Remove(entity);
        }

        public void Update(T entity)
        { 

           DbSet.Entry(entity).State = System.Data.Entity.EntityState.Modified;

        }

        public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
        {
            return DbSet.Where(predicate);
        }

        public IQueryable<T> GetAll()
        {
            return DbSet;
        }

        public T GetById(int id)
        {
            return DbSet.Find(id);
        }

        #endregion
    }

解决方案

Ok, I figured this out. The reason why there's no Update in new repository patterns (Entity Framework 6) is because there's no need for one. You simply call your record up by id, make your changes and then commit/save.

For example, this is my edit POST method from my postController:

[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Edit([Bind(Include = "Id,Title,IntroText,Body,Modified,Author")] Post post)
{
    using (UnitOfWork uwork = new UnitOfWork())
    {
        Post edit = uwork.PostRepository.GetById(post.Id);
        edit.Title = post.Title;
        edit.IntroText = post.IntroText;
        edit.Body = post.Body;
        edit.Modified = DateTime.Now;

        uwork.Commit();

        return RedirectToAction("Index");
    }
}

RepositoryPattern looks like this:

public class BlogEngineRepository<T> : IRepository<T> where T : class
    {
        protected DbSet<T> DbSet;

        public BlogEngineRepository(DbContext dataContext)
        {
            DbSet = dataContext.Set<T>();
        }

        #region IRepository<T> Members

        public void Insert(T entity)
        {
            DbSet.Add(entity);
        }

        public void Delete(T entity)
        {
            DbSet.Remove(entity); 
        }

        public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
        {
            return DbSet.Where(predicate);
        }

        public IQueryable<T> GetAll()
        {
            return DbSet;
        }

        public T GetById(int id)
        {
            return DbSet.Find(id);
        }

    }

这篇关于更新使用存储库模式记录与实体框架6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆