04.07.2013 Views

Programming Entity Framework - Cdn.oreilly.com

Programming Entity Framework - Cdn.oreilly.com

Programming Entity Framework - Cdn.oreilly.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

POCO objects do not <strong>com</strong>municate back to the context. Therefore, the context needs at<br />

some point to take a look at the POCO objects and synchronize their data with the<br />

ObjectStateEntry objects which represent them. The ObjectContext class has<br />

a method called DetectChanges that satisfies this purpose.<br />

Understanding the Importance of DetectChanges<br />

It is important to instruct the context to detect changes prior to constructing the various<br />

SaveChanges <strong>com</strong>mands when you want to send any changes made to your POCO<br />

objects to the database. Otherwise, the ObjectStateEntry objects that the context is<br />

managing will not reflect the changes and no insert, update, or delete <strong>com</strong>mands will be<br />

sent to the data store.<br />

You may recall from Chapter 5 that one of the SaveOptions parameters for<br />

SaveChanges is DetectAllChanges. That option will force the context to call<br />

DetectChanges prior to the save logic. The default behavior for SaveChanges is<br />

that it will call DetectChanges, so you do not need to explicitly call the method or set<br />

the SaveOptions enum.<br />

Exploring and Correcting POCOs’ Impact on<br />

Two-Way Relationships<br />

In addition to syncing up the ObjectStateEntry objects, the context will force the<br />

POCO classes to be aware of any two-way relationships. With an <strong>Entity</strong>Object<br />

class, if you add an address to the contact.Addresses <strong>Entity</strong>Collection, not<br />

only does that impact the Addresses property, but you also automatically get the twoway<br />

relationship fix-up. As a result, Address.Contact is also populated. The twoway<br />

relationship also works in the other direction. If you assign a contact instance to<br />

Address.Contact, that contact also recognizes that address in its Addresses<br />

<strong>Entity</strong>Collection.<br />

However, this doesn’t automatically happen with the POCO objects.<br />

Let’s modify the earlier code to see what happens when you build relationships with<br />

POCO objects. Add the code in Example 13-5 below the last line of code in Example 13-<br />

4. That line is included here for placement reference.<br />

Example 13-5. Experimenting with two-way relationships<br />

int addressCount = firstContact.Addresses.Count();<br />

//new code begins here<br />

Address newAddress = new Address<br />

{<br />

Street1 = "1 Main Street",<br />

City = "Mainville",<br />

StateProvince = "Maine",<br />

ModifiedDate =DateTime.Now<br />

};<br />

firstContact.Addresses.Add(newAddress);<br />

addressCount = firstContact.Addresses.Count;<br />

Contact newAddressContact = newAddress.Contact;<br />

//new code ends here

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

Saved successfully!

Ooh no, something went wrong!