作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个带有更新方法的 WCF 服务,用于更新数据库中的客户。
此方法从客户端获取一个分离的实体。
void UpdtaeCustomer(Customer detachedCustomer);
context.CustomerSet.Attach(detachedCustomer);
context.ObjectStateManager.ChangeObjectState(detachedCustomer, entityState.Modified);
context.SaveChanges();
Customer customer = context.GetObjectByKey(detachedCustomer.EntityKey);
context.ApplyCurrentValues<Customer>("CustomerSet", detachedCustomer);
context.SaveChanges();
最佳答案
使用第一种方法。对分离实体使用第二种方法没有普遍优势,相反它会使事情变得更糟。
假设您使用时间戳。时间戳是表示行版本的特殊 DB 类型。每次数据库中的记录更改时,时间戳都会自动增加。时间戳用于并发检查,当与 EF 一起使用时,它被处理为 Computed
柱子。每次 EF 想要更新记录时,它都会将数据库中的时间戳与您在加载对象时检索到的时间戳进行比较(必须在您的实体中传输到客户端并返回)。如果时间戳相同,则保存记录。如果它们不同,则抛出异常。
这两种方法之间的区别在于,第一种方法使用来自分离对象的时间戳,而第二种方法使用来自加载对象的时间戳。原因是计算列。无法在应用程序中更新计算值。
关于entity-framework-4 - Entity Framework 4 - ApplyCurrentValues<TEntity> 与 ObjectStateManager.ChangeObjectState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544849/
将 EF4 CTP5 迁移到 RC1 后,我注意到 ObjectContext 不再可通过 DbContext 访问。这意味着我无法访问 ChangeObjectState 方法。 class Dat
我们有一个带有更新方法的 WCF 服务,用于更新数据库中的客户。 此方法从客户端获取一个分离的实体。 void UpdtaeCustomer(Customer detachedCustomer); 我
我是一名优秀的程序员,十分优秀!