Updating records using a Repository Pattern with Entity Framework 6

前端 未结 2 829
半阙折子戏
半阙折子戏 2020-12-29 07:43

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:

相关标签:
2条回答
  • 2020-12-29 08:22

    Ok, I figured this out. The reason why there isn't an Update method in new repository patterns (Entity Framework 6) is because there's no need for one. You simply fetch your record 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>();
        } 
    
        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);
        } 
    }
    
    0 讨论(0)
  • 2020-12-29 08:33

    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;
      }
    }
    

    You can view a full explaination to the answer for Efficient way of updating list of entities for more information on the details of just an update.

    0 讨论(0)
提交回复
热议问题