简体   繁体   中英

Update record using MySQL, Entity Framework + Ext.NET

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?!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM