Dan*_*ieu 16 c# repository-pattern entity-framework-6 asp.net-mvc-5 asp.net-identity
我正在编写一个简单的博客应用程序,并尝试在我的通用存储库模式中建立CRUD操作,但我的更新方法出现错误,该错误说:
'System.Data.Entity.DbSet'不包含'Entry'的定义,并且没有扩展方法'Entry'可以找到接受类型'System.Data.Entity.DbSet'的第一个参数(你是否缺少using指令?或汇编参考?)
我跟着一篇文章解释了如何通过在DbContext上添加额外的间接级别来"伪造"Entry().但是在MVC 5中我们继承自:IdentityDbContext而不是DbContext.我确实试过实现作者修复,但错误仍然存在.
如何使用IdentityDbContext将更新方法添加到Entity Framework 6中的存储库?如果我们不应该这样做,那么如何使用这种模式更新记录?
我应该注意到其他所有方法都按预期工作.
我的通用存储库:
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
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*ieu 21
好的,我想通了.新存储库模式(实体框架6)中没有Update方法的原因是因为不需要一个.您只需按ID获取记录,进行更改然后提交/保存.
例如,这是我的postController中的编辑POST方法:
[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");
}
}
Run Code Online (Sandbox Code Playgroud)
RepositoryPattern看起来像这样:
public class BlogEngineRepository<T> : IRepository<T> where T : class
{
protected DbSet<T> DbSet;
public BlogEngineRepository(DbContext dataContext)
{
DbSet = dataContext.Set<T>();
}
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*ips 14
更新应该是这样的(扩展Dan Beaulieu的回答):
[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");
}
}
Run Code Online (Sandbox Code Playgroud)
RepositoryPattern看起来像这样:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以查看有关更新实体列表的有效方法的答案的完整说明,以获取有关更新详细信息的更多信息.