15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

882 ❘ ChaPTer 31 AdO.net entity frAmewOrk<br />

}<br />

}<br />

var r = entry.Entity as Racer;<br />

if (r != null)<br />

{<br />

Console.WriteLine("{0}: {1}", state, r.Lastname);<br />

}<br />

code snippet Formula1Demo/Program.cs<br />

When you run the application, the added <strong>and</strong> modified racers are displayed, <strong>and</strong> the properties changed<br />

with their original <strong>and</strong> current values are shown:<br />

Added: Alguersuari<br />

Modified: Alonso<br />

state of Fern<strong>and</strong>o: Modified<br />

modified: Starts<br />

original: 138<br />

current: 139<br />

attaching <strong>and</strong> detaching entities<br />

When returning entity data to the caller, it might be important to detach the objects from the object<br />

context. This is necessary, for example, if an entity object is returned from a Web service. Here, if the entity<br />

object is changed on the client, the object context is not aware of the change.<br />

With the sample code, the Detach() method of the ObjectContext detaches the entity named fern<strong>and</strong>o<br />

<strong>and</strong> thus the object context is not aware of any change done on this entity. If a changed entity object is<br />

passed from the client application to the service, it can be attached again. Just attaching it to the object<br />

context might not be enough because it doesn’t give the information that the object was modified. Instead,<br />

the original object must be available inside the object context. The original object can be accessed from the<br />

store by using the key with the method GetObjectByKey() or TryGetObjectByKey(). If the entity object<br />

is already inside the object context, the existing one is used; otherwise it is fetched newly from the database.<br />

Invoking the method ApplyCurrentValues() passes the modified entity object to the object context,<br />

<strong>and</strong> if there are changes, then the changes are done within the existing entity with the same key inside<br />

the object context, <strong>and</strong> the EntityState is set to EntityState.Modified. Remember that the method<br />

ApplyCurrentValues() requires the object to exist within the object context; otherwise the new entity<br />

object is added with EntityState.Added:<br />

using (var data = new Formula1Entities())<br />

{<br />

data.ObjectStateManager.ObjectStateManagerChanged +=<br />

ObjectStateManager_ObjectStateManagerChanged;<br />

ObjectQuery racers = data.Racers.Where("it.Lastname='Alonso'");<br />

Racer fern<strong>and</strong>o = racers.First();<br />

EntityKey key = fern<strong>and</strong>o.EntityKey;<br />

data.Racers.Detach(fern<strong>and</strong>o);<br />

}<br />

// Racer is now detached <strong>and</strong> can be changed independent of the object context<br />

fern<strong>and</strong>o.Starts++;<br />

Racer originalObject = data.GetObjectByKey(key) as Racer;<br />

data.Racers.ApplyCurrentValues(fern<strong>and</strong>o);<br />

code snippet Formula1Demo/Program.cs<br />

storing entity Changes<br />

Based on all the change information with the help of the ObjectStateManager, the added, deleted, <strong>and</strong><br />

modified entity objects can be written to the store with the SaveChanges() method of the ObjectContext<br />

class. To verify changes within the object context, you can assign a h<strong>and</strong>ler method to the SavingChanges<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!