我有一个关于在带有 Entity Framework Core 的 MVC Web 应用程序中使用存储库模式和工作单元模式的问题。
我目前正在控制器中实现更新功能。现在,此时我不确定更新实体的最佳方法是什么。我看过一些视频,他们说像这样的存储库中不应存在 Update 方法:
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)
所以这意味着我必须在控制器中这样做:
public IActionResult Edit(int id, [Bind("NeighbourhoodGroup,Neighbourhood,NeighbourhoodId")] Neighbourhoods neighbourhoods)
{
var neighbourhoodsFound = unitOfWork.Neighbourhoods.Get(id);
neighbourhoodsFound.Neighbourhood = neighbourhoods.Neighbourhood;
neighbourhoodsFound.NeighbourhoodGroup = neighbourhoods.NeighbourhoodGroup;
}
Run Code Online (Sandbox Code Playgroud)
但是,这意味着我必须在所有控制器中都这样做,即使对象有很多属性?
我希望有人可以就最好的方法给我一些建议。