I get the following exception when trying to update my records:
System.InvalidOperationException: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
The code is as follows: (player is a contract version of player which is passed to method as parameter, ToDbPlayer() is an extension method that takes a contract.Player object and creates an equivalent one for the DB)
using (var context = _contextFactory.CreateEntities())
{
var dbPlayer = context.Players.Find(player.PlayerId);
var entity = context.Players.Attach(player.ToDbPlayer()); //here error occurs
context.Entry(entity).State = dbPlayer == null ? EntityState.Added : EntityState.Modified;
context.SaveChanges();
}
I'm confused as to what to do - I'm trying to simply update the records in the DB however its falling over when I try to attach it to the context.
I'm not overly confident on my EF skills so if someone can point me in the right direction, it'd be appreciated.