0

I'm new to Entity Framework and I have the following case:

I have two entities: - Solution - Category

A Category can have a lot of Solutions and a Solution can have many categories. I'm receiving via REST an updated Solution containing the list of categories now belonging to the solution, but I can't seem to update the relashionship:

public async Task<IHttpActionResult> PutSolution(int id, Solution solution)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        if (id != solution.ID)
            return BadRequest();

        if (!SolutionExists(id))
            return NotFound();

        db.Entry(solution).State = EntityState.Modified;


        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            throw;
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

Code is breaking on db.Entry(solution).State = EntityState.Modified with the message:

Attaching an entity of type 'ForgeITAPI.Models.Solution' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

EDIT: REST object:

{
   "id":7,
   "title":"New Solution",
   "resume":"teste",
   "fullDescription":"<p>teste sdgrasdfgfdashfdsh<br></p>",
   "categories":[
      {
         "id":1,
         "name":"Augmented Reality",
         "solutions":[
            {
               "id":1,
               "title":"teste",
               "resume":"This project is awesome. 1",
               "fullDescription":"<p>Teste<br></p>",
               "categories":[

               ],
               "multimedia":null,
               "bulletPoints":null,
               "creationDate":"1900-01-01T00:00:00",
               "state":0
            }
         ],
         "newsPosts":[

         ],
         "$$hashKey":"object:16"
      },
      {
         "id":4,
         "name":"Virtual Reality",
         "solutions":[
            {
               "id":1,
               "title":"teste",
               "resume":"This project is awesome. 1",
               "fullDescription":"<p>Teste<br></p>",
               "categories":[
                  {
                     "id":1,
                     "name":"Augmented Reality",
                     "solutions":[

                     ],
                     "newsPosts":[

                     ]
                  }
               ],
               "multimedia":null,
               "bulletPoints":null,
               "creationDate":"1900-01-01T00:00:00",
               "state":0
            }
         ],
         "newsPosts":[

         ],
         "$$hashKey":"object:17"
      }
   ],
   "multimedia":[

   ],
   "bulletPoints":null,
   "creationDate":"1900-01-01T00:00:00",
   "state":0,
   "status":"Draft",
   "creationDateString":"1 of January 1900",
   "$$hashKey":"object:9"
}
Ricardo Alves
  • 1,071
  • 18
  • 36
  • There are a few problems here, but first things first: 1) does your `SolutionExists` method load a `Solution` from the same `db`? 2) What is the expected behaviour of the REST request you posted - in particular, how many `Category` objects are you expecting to be **created**? – Iain Galloway Oct 31 '17 at 12:48
  • The solution Exists just check if the ID already is on the database. This methos is suppose to update only the "Solutions" object and it's relashionships. So, in this example, the categories passed already exist on the database. – Ricardo Alves Oct 31 '17 at 13:01
  • How does the `SolutionExists` method check whether the Id already exists? Does it use the same instance of `db` that this method uses? Does it load the matching `Solution` from that instance of the context, such that there's already an entry with that primary key value? – Iain Galloway Oct 31 '17 at 13:06
  • private bool SolutionExists(int id) { return db.Solutions.Count(e => e.ID == id) > 0; } – Ricardo Alves Oct 31 '17 at 13:12
  • So, the problem is that you're trying to `Attach` an object graph that contains multiple references to the same object - in this case, it's probably the `Solution` with id = 1 that EF is complaining about, but you'll notice that you're also attaching the `Category` with id = 1 twice too. Generally you don't update M:M relationships like this - by providing a pre-filled collection and saying "make the collection in the database the same as this collection". There's a good answer with several examples here:- https://stackoverflow.com/questions/8869632 – Iain Galloway Oct 31 '17 at 13:27

0 Answers0