I have a question about using the Repository Pattern and Unit of Work pattern in a MVC Web Application with Entity Framework Core.
(我有一个关于在带有Entity Framework Core的MVC Web应用程序中使用存储库模式和工作单元模式的问题。)
I am currently implementing the update functionality in the controller.
(我目前正在控制器中实现更新功能。)
Now, at this point I am not sure what the best way is to update the entity. (现在,我不知道最好的方法是更新实体。)
I have watched some videos where they said that an Update method should not be present in a repository like this: (我看了一些视频,他们说更新方法不应出现在这样的存储库中:)
public T Update(T entity)
{
DbSet.Attach(entity);
var entry = Context.Entry(entity);
entry.State = System.Data.EntityState.Modified;
}
So that means that I will have to do it like this in the controller:
(因此,这意味着我将必须在控制器中这样做:)
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;
}
However, that means that I will have to do this in all controllers, even if the object has alot of Properties?
(但是,这意味着即使对象具有很多属性,我也必须在所有控制器中执行此操作?)
I hope someone can give me some advice on what the best approach would be.
(我希望有人能给我一些关于最佳方法的建议。)
ask by Azzaronn translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…