I want to update record from FormView
with ObjectDataSource
and lose my day to solve this error.
An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied.
My code is below
private static Entities1 _db = null;
public static Entities1 CreateDataContext()
{
if (_db == null)
{
_db = new Entities1(System.Configuration.ConfigurationManager.ConnectionStrings["Entities1"].ConnectionString);
_db.games.MergeOption = MergeOption.NoTracking;
_db.my_aspnet_users.MergeOption = MergeOption.NoTracking;
_db.platforms.MergeOption = MergeOption.NoTracking;
}
return _db;
}
public void Update(game item)
{
Entities1 DB = CreateDataContext();
item.modified = DateTime.Now;
var obj = (from u in DB.games
where u.idgames == item.idgames
select u).First();
DB.games.ApplyCurrentValues(item);//Error Here
DB.SaveChanges();
}