I have this Controller action:
[HttpGet]
[Route("api/person/test/{id}")]
public IHttpActionResult Test(Guid id)
{
var person = this.PersonManager.Get(id);
person.Lastname = "Jameson";
this.PersonManager.Save(person);
return Ok(true);
}
Deep underneath this save method is called:
protected void Add<T>(T source, MyEntities context, bool isNew) where T : class
{
if (isNew)
{
context.Set<T>().Add(source);
}
else
{
var entry = context.Entry(source);
if (entry.State == EntityState.Detached)
{
context.Set<T>().Attach(source);
entry.State = EntityState.Modified;
}
}
}
I get this error when executing this:
Attaching an entity of type 'Model.Person' 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.
It occurs on the context.Set<T>().Attach(source)
line.
PS: This is a followup/spin off of this question.
context.person.Local
to see if have one person object which have the same primary with the updated person object? – Shawn