实体框架4 - ApplyCurrentValues< TEntity> vs. ObjectStateManager.ChangeObjectState [英] Entity Framework 4 - ApplyCurrentValues<TEntity> vs. ObjectStateManager.ChangeObjectState

查看:154
本文介绍了实体框架4 - ApplyCurrentValues< TEntity> vs. ObjectStateManager.ChangeObjectState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个更新方法的WCF服务来更新数据库中的一个客户端。
此方法从客户端获取一个分离的实体。

  void UpdtaeCustomer(Customer detachedCustomer); 

我们提出了两种方法来写这个方法:



1)

  context.CustomerSet.Attach(detachedCustomer); 
context.ObjectStateManager.ChangeObjectState(detachedCustomer,entityState.Modified);
context.SaveChanges();

2)

 code> Customer customer = context.GetObjectByKey(detachedCustomer.EntityKey); 
context.ApplyCurrentValues< Customer>(CustomerSet,detachedCustomer);
context.SaveChanges();

我们要考虑每种方法的cons \pros。第一个有一个明显的优势,只有一次旅行到DB。但是第二种方法的优点是什么? (或者也许他们的行为不一样)?

解决方案

使用第一种方法。使用第二种方法与分离实体没有一般的优势,相反它可以使事情更糟。



假设您使用时间戳。时间戳是表示行版本的特殊DB类型。每次数据库中的记录更改时间戳都会自动增加。 Timestamp用于concurrecny检查,当与EF一起使用时,它被处理为 Computed 列。每次EF要更新记录时,将数据库中的时间戳与您加载对象时检索到的时间戳进行比较(必须在实体中传输到客户端并返回)。如果时间戳相同,则保存记录。如果它们不同,则抛出异常。



这两种方法之间的区别是第一种方法使用分离对象的时间戳,而第二种方法使用来自加载对象的时间戳。原因是计算列。计算值无法在应用程序中更新。


We have an WCF service with an update method that updates a Customer in the DB. This method get a detached entity from the client.

void UpdtaeCustomer(Customer detachedCustomer);

We came up with two ways to write this method :

1)

context.CustomerSet.Attach(detachedCustomer);
context.ObjectStateManager.ChangeObjectState(detachedCustomer, entityState.Modified);
context.SaveChanges();

2)

Customer customer = context.GetObjectByKey(detachedCustomer.EntityKey);
context.ApplyCurrentValues<Customer>("CustomerSet", detachedCustomer);
context.SaveChanges();

We want to consider the cons\pros of each method. The first one has a clear advantage of having only one trip to the DB. But what are the pros of the second method. (or maybe they don't act quite the same) ?

解决方案

Use the first approach. There is no general advantage in using second method with detached entity on the contrary it can make things even worse.

Suppose that you use timestamp. Timestamp is special DB type representing row version. Each time the record in database changes the timestamp is automatically increased. Timestamp is used for concurrecny checks and when using with EF it is handled as Computed column. Each time the EF wants to update the record it compares timestamp in database with the timestamp you retrieved when you loaded the object (must be transpored in your entity to client and back). If timestamps are same the record is saved. If they are different an exception is thrown.

The difference between those two methods is that first method uses timestamp from detached object whereas second method uses timestamp from loaded object. The reason is the computed column. Computed values can't be updated in the application.

这篇关于实体框架4 - ApplyCurrentValues&lt; TEntity&gt; vs. ObjectStateManager.ChangeObjectState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆