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"
}