0

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();         
}

1 Answers1

6

In your method:

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();         
}

item is not attached so it can't be updated. That's pretty much what the error message is telling you, too.

It looks like you'd want to use obj which is retrieved from your context. Then set the values of obj to those in item, and use obj to make the updates.

EDIT for sample...

If you just want to set the modified date and time you'd do this:

public void Update(game item) {
    Entities1 DB = CreateDataContext();

    var obj = (from u in DB.games
               where u.idgames == item.idgames
               select u).SingleOrDefault();
    if (obj == null) {
      // handle the case where obj isn't found
      // probably by throwing an exception
    }

    obj.modified = DateTime.Now;
    DB.games.ApplyCurrentValues(obj);
    DB.SaveChanges(); 
}
Yuck
  • 49,664
  • 13
  • 105
  • 135
  • Take a look at this http://msdn.microsoft.com/en-us/library/bb896271(v=VS.100).aspx In general you will call `Attach()` or you can query the context for an object which will return it in the attached state. For instance, `obj` in your example will come back attached. – Yuck Dec 15 '11 at 14:52
  • i have many Classes which was created in nhibernat so now i am converting all to .Net 4.0. if i create obj and ill pass manually then it ill time consume process. I want to update only with item coming from ObjectDataSource. – Muhammad Imran Dec 15 '11 at 14:52
  • Hi Yuck, Please can you give me a good example to attach. i tried some samples but failed. thanks – Muhammad Imran Dec 15 '11 at 15:00
  • thanks to reply. Update(game item)....item Object holds all changes it has also childs tables which are changed from objectdatasource. so then i want to update that item object rather than obj. – Muhammad Imran Dec 15 '11 at 16:23
  • Then `Attach(item)` and don't bother bringing `obj` into the state manager. – Yuck Dec 15 '11 at 16:28
  • i try to attach. DB.games.Attach(item);......it throws exception....A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship. – Muhammad Imran Dec 15 '11 at 19:21