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

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

问题描述

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


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


I后跟一个发布说明如何假 Entry()通过在DbContext上添加附加的间接级别。但是在MVC 5中,我们继承自: IdentityDbContext ,而不是DbContext。我确实尝试实现作者修复,但错误仍然存​​在。



我的问题



如何添加更新方法到Entity Framework 6中使用IdentityDbContext的存储库?如果我们不应该这样做,那么如何更新这个模式的记录?



我应该注意,所有其他方法都按照预期工作。



我的通用存储库:

 公共类BlogEngineRepository< T> :IRepository< T>其中T:class 
{
protected DbSet< T> DbSet;

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

#region IRepository< T>成员

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。改性;

}

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

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

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

#endregion
}


解决方案

更新应该是(扩展Dan Beaulieu的答案):

$
[ValidateInput(false)]
public ActionResult Edit([Bind( Include =Id,Title,IntroText,Body,Modified,Author)]帖子)
{
using(UnitOfWork uwork = new UnitOfWork())
{
post。 Modified = DateTime.Now;
uwork.PostRepository.Update(post);

uwork.Commit();

return RedirectToAction(Index);
}
}

RepositoryPattern如下所示:

  public class BlogEngineRepository< T> :IRepository< T>其中T:class 
{
public BlogEngineRepository(DbContext dataContext)
{
DbSet = dataContext.Set&T;();
Context = dataContext;
}

public T Update(T entity)
{
DbSet.Attach(entity);
var entry = Context.Entry(entity);
entry.State = System.Data.EntityState.Modified;
}
}


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
    }

解决方案

Update should look like (expanding on Dan Beaulieu's answer) :

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

        uwork.Commit();

        return RedirectToAction("Index");
    }
}

RepositoryPattern looks like this:

public class BlogEngineRepository<T> : IRepository<T> where T : class
{
  public BlogEngineRepository(DbContext dataContext)
  {
    DbSet = dataContext.Set<T>();
    Context = dataContext;
  }

  public T Update(T entity)
  {
     DbSet.Attach(entity);
     var entry = Context.Entry(entity);
     entry.State = System.Data.EntityState.Modified;
  }
}

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

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