ObjectContext未检索最近的更改

时间:2012-08-07 18:49:49

标签: asp.net-mvc-3 entity-framework telerik-grid

这段代码工作正常,但是,如果我在我的工作单元(或任何更新,删除,添加操作)中运行执行storedprocedure,我仍然会获得原始数据。实际上,我已经有了一个解决方案(在控制器下面发布)但我确定这不是最优雅的方式,我希望有人可以帮我重构代码。请帮忙。感谢

我的工作单元

 public class UnitOfWork : IUnitOfWork, IDisposable
 {
   private readonly ObjectContext _context;
   private BookRepository _books;

    public UnitOfWork(ObjectContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("Context was not supplied");
        }

        _context = context;
    }

    public IRepository<Book> Books
    {
        get
        {
            if (_books== null)
            {
                _books= new BookRepository (_context);
            }

            return _books;
        }
    }

    public void UpdateAuthor(int id)
    {
        _context.ExecuteStoreCommand("sp_UpdateAuthor @param1", 
            new SqlParameter("param1", id));
     }

     public void Commit()
    {           
       _context.SaveChanges();
    }

图书存储库

public class BookRepository : Repository<Book>
{
    public BookRepository (ObjectContext context)
        : base(context)
    {
    }

    public override Machine GetById(object id)
    {
        return _objectSet.SingleOrDefault(s => s.Id== (int)id);
    }
}

通用存储库

public abstract class Repository<T> : IRepository<T>
    where T : class, IAuditEntity
{
    protected IObjectSet<T> _objectSet;

    public Repository(ObjectContext context)
    {
        _objectSet = context.CreateObjectSet<T>();
    }

    public abstract T GetById(object id);

    public IEnumerable<T> GetAll()
    {
        return _objectSet;
    }

    public IEnumerable<T> Query(Expression<Func<T, bool>> filter)
    {
        return _objectSet.Where(filter);
    }

    public void Add(T entity)
    {
        _objectSet.AddObject(entity);
    }

    public void Remove(T entity)
    {            
        _objectSet.DeleteObject(entity); 
    }
  }

控制器代码

 public class HomeController : Controller
  {
    private IUnitOfWork _unitOfWork;

    #region Core Action Methods

    public HomeController()
    {
        this._unitOfWork = new UnitOfWork(((IObjectContextAdapter)new BookContext()).ObjectContext);
    }

  private IEnumerable<BookViewModel> GetBookdsViewModels(int id)
    {
        //THE CODE WHERE ITS NOT RETURNING THE UPDATED VLAUES
        //var query = _unitOfWork.Books.GetAll().Where(d => d.Id== id);

        //I WANT TO CHANGE THIS CODE
        ObjectContext objectContext = ((IObjectContextAdapter)new BookContext()).ObjectContext;
        ObjectSet<Book> set = objectContext.CreateObjectSet<Book>();
        set.MergeOption = MergeOption.OverwriteChanges;
        var query = from a in set
                    where a.Id== id && !a.IsDeleted
                    select a;

 return query
            .Select(
            c => new BookViewModel
            {
                Id = c.Id ,
                Name = c.Name
            });
}

2 个答案:

答案 0 :(得分:1)

我认为问题是因为您直接针对数据库执行操作,然后尝试返回存储在存储库中的本地副本,它们是不同的。

public void UpdateAuthor(int id)
    {
        _context.ExecuteStoreCommand("sp_UpdateAuthor @param1", 
            new SqlParameter("param1", id));
     }

当你运行这个查询时,你在db而不是你的本地进行了更改 - 为什么你不这样做:

public void UpdateAuthor(int id)
{
  var book  = Books.GetById(id);
   /* make changes to your book object */
   _unit.Save();
}

如果您想使用存储过程进行更改,则必须处理您的上下文,并重新创建存储库,以便您使用来自数据库的数据本地副本。

答案 1 :(得分:1)

扩展Mark Oreta的答案,您需要确保在数据库上手动执行存储过程后更新模型。如果您必须手动调用该存储过程,请在以后尝试:

_context.Entry<Book>(instanceOfAuthor).Reload();

可能是:

_context.Entry<Book>(_context.Books.GetById(id)).Reload();