I'm using the following GitHub project for generic repository and UoW pattern
https://genericunitofworkandrepositories.codeplex.com/
[HttpPost]
[Route("update")]
public HttpResponseMessage Update(HttpRequestMessage request, ComponentViewModel component)
{
return CreateHttpResponse(request, () =>
{
HttpResponseMessage response = null;
if (!ModelState.IsValid)
{
response = request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
else
{
var componentDb = UnitOfWork.Repository<Component>().Find(component.ID);
if (componentDb == null)
response = request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid component.");
else
{
componentDb = Mapper.Map<ComponentViewModel, Component>(component);
UnitOfWork.Repository<Component>().Update(componentDb); // <-- ERROR'S HERE
UnitOfWork.SaveChanges();
response = request.CreateResponse<ComponentViewModel>(HttpStatusCode.OK, component);
}
}
return response;
});
}
I get the following exception at UnitOfWork.Repository<Component>().Update(componentDb);
Attaching an entity of type 'Component' failed because another entity of the same type already has the same primary key value
I believe it is due to the AutoMapper Mapper.Map code before it, but, I'm not sure how to correct that.
Please advise how to correct the usage.