Why I do I fall into all of the hurdles for a simple update in EF?

Viewed 359

I have an ID with me and I have name with me. So in essence, my method just has these parameters:

public void Foo(int id, string name)
{
}

and I have this piece of logic inside method:

User user = new User(){ Id = id, Name = name };
Db.Entry(user).State = System.Data.EntityState.Modified;
Db.SaveChanges();

That's it. Nothing fancy. I get this error: "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key"

and this answer by Ladislav Mrnka: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

suggests to use context.Entry(oldEntity).CurrentValues.SetValues(newEntity); but I don't really have oldEntity with me. Can anybody just please tell me how do I update just 1 property of User? I am getting nuts.

1 Answers
Related