简体   繁体   English

使用MySQL,实体框架+ Ext.NET更新记录

[英]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. 检查ObjectStateManager确认这一点。 In general, you must modify properties after you attach the object. 通常,附加对象必须修改属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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