2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
billboard
  • 785
  • 4
  • 13
  • 25
  • Please have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:27

1 Answers1

3

That is because you are using Find method. This method is going to attach the returned entity to your context, later using Automapper you are creating a disconnected POCO entity, which you are trying later to attach to your context with Update method of your generic repository, and both entities share the same Id. Use Any extension method instead of Find to check if there is an entity with that Id in your table:

 if (UnitOfWork.Repository<Component>().Any(c=>c.Id==component.ID))// Call Any here
 {
       componentDb = Mapper.Map<ComponentViewModel, Component>(component);
       UnitOfWork.Repository<Component>().Update(componentDb); 
       UnitOfWork.SaveChanges();
       response = request.CreateResponse<ComponentViewModel>(HttpStatusCode.OK, component);
 }
 else
 {
      response = request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid component.");
 }  
ocuenca
  • 38,548
  • 11
  • 89
  • 102