Entity Framework 4 - ApplyCurrentValues ββ<TEntity> compared to ObjectStateManager.ChangeObjectState
We have a WCF service with an update method that updates the client in the database. This method receives an individual client from the client.
void UpdtaeCustomer(Customer detachedCustomer); We came up with two ways to write this method:
one)
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 cons \ pros for each method. The first has the clear advantage of having only one trip to the database. But what are the advantages of the second method. (or maybe they do not act the same)?
Use the first approach. There is no general advantage to using the second method with a separate object; on the contrary, this can make it even worse.
Suppose you are using a timestamp. Timestamp is a special type of DB representing the version of a string. Each time a record in the database changes, the time stamp automatically increases. The timestamp is used to verify concurrecny and, when used with EF, is treated as a Computed column. Each time EF wants to update a record, it compares the timestamp in the database with the timestamp that you retrieved when you downloaded the object (it must be transferred to your object to the client and vice versa). If the time stamps are the same, the recording is saved. If they differ from each other, an exception is thrown.
The difference between the two methods is that the first method uses a timestamp from an individual object, while the second method uses a timestamp from a loaded object. The reason is the calculated column. The calculated values ββcannot be updated in the application.