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

Create successful ePaper yourself

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

able to tap into the same set of services that the <strong>Entity</strong>Object has access to. The<br />

POCO class now has access to these services and can therefore interact with the<br />

ObjectContext in a similar fashion as the <strong>Entity</strong>Object.<br />

Synchronizing Relationships by Proxy<br />

Finally, we can return to the third method of fixing up two-way relationships. With<br />

proxies, this also benefits classes with both a foreign key and related navigation property<br />

instance (e.g., Address.ContactID and Address.Contact) because the proxy<br />

will synchronize them. You may recall seeing <strong>Entity</strong>Objects do this in Chapter 10.<br />

First let’s look at a scenario where you are linking two existing entities. The following<br />

code queries for a random Contact and an Address and joins them:<br />

Address address = context.Addresses.<br />

Where(a=>a.City=="Winnipeg").FirstOrDefault();<br />

Contact contact = context.Contacts.FirstOrDefault();<br />

contact.Addresses.Add(address);<br />

If you are not using the proxy behavior (i.e., the properties are not marked as virtual),<br />

then after this code is run, address.Contact and address.ContactID will be<br />

null.<br />

If you have enabled the proxy to work, address.Contact will point to the contact<br />

and address.ContactID will have the correct value.<br />

If you are creating new objects and you want the relationships to be fixed up there is<br />

another important rule to know about.<br />

You might just create a new address by instantiating it:<br />

Address address = new Address();<br />

The context will have absolutely no clue about this address, and if you added it to<br />

contact.Addresses, you would not get the fix-up behavior.<br />

You need to let the context instantiate the object for you:<br />

Address address = context.CreateObject();<br />

Then when you add this address to the collection, or set address.Contact to the<br />

existing contact, the relationship and foreign key will be automatically fixed.<br />

If you are joining two new objects that were created with CreateObject, you will still<br />

get the fix-up behavior, but remember that the foreign key value (e.g., ContactID) will<br />

be 0 since it is unassigned. But that is still different from null, which is what you would<br />

get when the fix-up is not occurring at all.<br />

The Critical Rules for Getting Proxy<br />

Behavior with POCO Objects<br />

I pointed out three critical rules in the previous text that are worthy of<br />

highlighting along with some others that are equally important.<br />

Rule 1: To get the proxy behavior for a POCO object, every single property<br />

(scalar and navigation properties) must be made virtual and public using the C#<br />

virtual keyword or the VB Overridable keyword.

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

Saved successfully!

Ooh no, something went wrong!