【发布时间】:2013-02-20 16:19:53
【问题描述】:
帮帮我,我真的很困惑!
我只是想更新一些东西!这是我的控制器(后期操作):
[HttpPost]
public ActionResult Edit(CategoryViewModel categoryViewModel)
{
if(ModelState.IsValid)
{
_categoryService.UpdateCategory(categoryViewModel.Id);
}
return View();
}
这是我的服务类(我的问题是关于这个类,我不知道如何更新它)
public CategoryViewModel UpdateCategory(Guid categoryId)
{
var category = _unitOfWork.CategoryRepository.FindBy(categoryId);
var categoryViewModel = category.ConvertToCategoryViewModel();
_unitOfWork.CategoryRepository.Update(category);
_unitOfWork.SaveChanges();
return categoryViewModel;
}
最后我的基础存储库是这样的:
private readonly DbSet<T> _entitySet;
public void Update(T entity)
{
_entitySet.Attach(entity);
}
我的UnitOfWork也是这个:
public class UnitOfWork : IUnitOfWork
{
private IRepository<Category> _categoryRepository;
public IRepository<Category> CategoryRepository
{
get { return _categoryRepository ?? (_categoryRepository = new Repository<Category>(_statosContext)); }
}
}
【问题讨论】:
-
更新
category对象属性的代码在哪里? -
另外,我假设您的
_unitOfWork变量是DbContext或ObjectContext派生对象。真的吗?如果不是,您的SaveChanges函数的主体是什么? -
是的,我的 _UnitOfWork variabel id DbCotext,但我的麻烦在于服务类,基本上我的算法有问题,虽然我也用我的 UnitOfwork 编辑我的问题
-
正如我提到的,我的问题在于 UpdateCategory(Guid categoryId),我不知道如何通过获取较早的名称并用新的替换来更新类别的名称...跨度>
-
再次,在您的
UpdateCategory中,我看不到您在哪里更新了category变量的属性。这是在哪里发生的?如果您从不更新属性,那么在调用SaveChanges时 EF 将不执行任何操作。
标签: c# asp.net-mvc-3 entity-framework