Friends, the scenario is this: when I want to edit a record in my BD, I'm using context.Entry(entidad).State = EntityState.Modified;
; the problem arises that when you get to this line, you throw this exception to me
Attaching an entity of type 'MODEL' failed because another entity of the same type has already 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.
Finding out about it, this makes reference to that in theory I have two entities in the memory of my context with the same PK
(or is what I understood).
The update flow that I am applying is the following (roughly):
public bool Save(MiEntidad entity){
if(entity.id > 0){
context.Entry(entidad).State = EntityState.Modified;
} else {
db.add(entity);
}
return this.context.SaveChanges() > 0;
}
I tried getting the record that I want to update from my database and adding context.Entry(origin).State = EntityState.Detached;
but the problem persists.
It is worth mentioning that if the entity is an update (entity.id> 0) I perform validations in which I assign ID to each child entity, but when doing this I use the parameter entity so I'm not using the context of my BD.
Any guidance would be appreciated!