Question

I have a snippet of code that I can't quite get to work:

StoreDataHandler dataHandler = new StoreDataHandler(HttpContext.Request["data"]);
ChangeRecords<ChequeDiary> data = dataHandler.ObjectData<ChequeDiary>();

foreach (ChequeDiary item in data.Updated) {
    db.ChequeDiaries.Attach(item);
    db.Refresh(System.Data.Objects.RefreshMode.ClientWins, item);
}

This is meant to get the changes and update the underlying object but when I call

db.SaveChanges();

.. nothing is updated. If I use:

foreach (ChequeDiary item in data.Updated) {
    ChequeDiary obj = db.ChequeDiaries.FirstOrDefault(o => o.Id == item.Id);
    obj.BankedAmount = item.BankedAmount;
}

and explicity set each property, it works. Why?!

Was it helpful?

Solution

Your first example likely doesn't mark any properties as modified. Check the ObjectStateManager to confirm this. In general, you must modify properties after you attach the object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top