In multi-Tier Application (N-Tier) environments, how to update objects is a complex topic. This article discusses how the latest ADO. NET Entity Framework 4 Beta 2 (EF4) implements object update in multi-tier applications and the middle layer. The following example shows a Web application (such as ASP. net mvc) is particularly useful for reference, because in the Web environment, the middle layer and the client cannot share object objects, and thus cannot use the tracking object changes.
The new EF4 provides many new tools and methods for multi-tier application environments, including ApplyCurrentValues, ApplyOriginalValues, SetModifiedProperty, GetUpdatableOriginalValues, and CurrentValues attributes. For example, in EF 1, if you want to mark an object as a "modified" state, you must call SetModified on the attributes of the object itself and the object. Now you only need to call the ChangeObjectState of ObjectStateManager, you can mark all attributes of an Attached object as "modified.
Example
In the following example, the new and old values of the Product instance are passedUpdateProductChanges method to avoid querying the database or saving the old value of the object to be updated in the memory. For ASP. net mvc, The originalProduct object can be stored in a Hidden element of the Form. When submitting the Form, it is passed to the Action.
Code
1 private static void ApplyProductUpdates (Product originalProduct,
2 Product updatedProduct)
3 {
4 using (NorthwindEntities northwindContext =
5 new NorthwindEntities ())
6 {
7 try
8 {
9 // Attach the original item to the object context.
10 northwindContext. Attach (originalProduct );
11
12 // Call the ApplyCurrentValues method to apply changes
13 // from the updated item to the original version.
14 northwindContext. ApplyCurrentValues ("Products ",
15 updatedProduct );
16
17 northwindContext. SaveChanges ();
18}
19 catch (InvalidOperationException ex)
20 {
21 Console. WriteLine (ex. ToString ());
22}
23}
24}
The following example shows how to update an object without providing the old object:
Code
1 private static void ApplyProductUpdates (Product updatedProduct)
2 {
3 // Define an ObjectStateEntry and EntityKey for the current object.
4 EntityKey key;
5 object originalProduct;
6
7 using (NorthwindEntities northwindContext =
8 new NorthwindEntities ())
9 {
10 try
11 {
12 // Create the detached object's entity key.
13 key = northwindContext. CreateEntityKey ("Products", updatedProduct );
14
15 // Get the original item based on the entity key from the context
16 // or from the database.
17 if (northwindContext. TryGetObjectByKey (key, out originalProduct ))
18 {
19 // Call the ApplyCurrentValues method to apply changes
20 // from the updated item to the original version.
21 northwindContext. ApplyCurrentValues (
22 key. EntitySetName, updatedProduct );
23}
24
25 northwindContext. SaveChanges ();
26}
27 catch (InvalidOperationException ex)
28 {
29 Console. WriteLine (ex. ToString ());
30}
31}
32}
[Update]
In the second method above, the original object value is obtained from the database or the current Context, in ASP. net mvc usually creates a new Context in Action. In this application environment, the original values are basically obtained from the database. Is there any way to neither provide the original object value nor avoid the extra load of retrieving the original value from the database?
The new EF4 provides us with a convenient method,
Code
1 private static void ApplyProductUpdates (Product updatedProduct ){
2 using (NorthwindEntities northwindContext = new NorthwindEntities ()){
3 try {
4 northwindContext. Attach (updatedProduct );
5 northwindContext. ObjectStateManager. ChangeObjectState (updatedProduct, EntityState. Modified );
6
7 northwindContext. SaveChanges ();
8}
9 catch (InvalidOperationException ex ){
10 Console. WriteLine (ex. ToString ());
11}
12}
13}
In EF1, You need:
Code
1 private static void ApplyProductUpdates (Product updatedProduct ){
2 using (NorthwindEntities northwindContext = new NorthwindEntities ()){
3 try {
4 northwindContext. Attach (updatedProduct );
5 var stateEntry = context. ObjectStateManager. GetObjectStateEntry (updatedProduct );
6 foreach (var propertyName in from fm in stateEntry. CurrentValues. DataRecordInfo. FieldMetadata select fm. FieldType. Name ){
7 stateEntry. SetModifiedProperty (propertyName );
8}
9
10 northwindContext. SaveChanges ();
11}
12 catch (InvalidOperationException ex ){
13 Console. WriteLine (ex. ToString ());
14}
15}
16}