Below is the code of my API, but it always returns a 500 Internal server error. The exception is thrown when executing SaveChangesAsync()
.
The instance of entity type 'NewsReport' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values
[HttpPut("{id}")]
public async Task<IActionResult> EditDevblog([FromBody] DevblogModel devblog, int id)
{
if (!ModelState.IsValid)
return BadRequest();
var devblogInDb = _context.DevblogModels
.Include(d => d.Fixs)
.Include(d => d.News)
.Include(d => d.Removes)
.Include(d => d.Updates)
.SingleOrDefault(d => d.Id == id);
if (devblogInDb == null)
return NotFound();
devblogInDb.Fixs = devblog.Fixs;
devblogInDb.News = devblog.News;
devblogInDb.Removes = devblog.Removes;
devblogInDb.Updates = devblog.Updates;
devblogInDb.PatchName = devblog.PatchName;
await _context.SaveChangesAsync();
var h = _context.DevblogModels.SingleOrDefault(d => d.Id == id);
return Ok();
}