Entity Framework relations and entityKey
Suppose you generate an Entity Framework model on standard northwind database, you have a customer id and you want to generate an order for that customer. A possible solution is
|
|
This code is perfectly valid, but is not the smartest thing to do, if you analyze traffic with profiler you can verify that this code executes a select to retrieve the customer, and then inserts the order into orders table. The question is, since I already have the ID of the customer why I need to retrieve the whole customer instance from database only to set a relation?. When you need to setup a relations EF needs only to know the key of related object, it does not really cares about other properties of Customers object, it needs only the key to set the foreign key, a better solution is the following
|
|
The core difference is in lines 6-10, I simply create an EntityKey object for the “ModelTestBase.Customers” object specifying its key value. This code inserts the order and sets the relation without the need to load the customer object. This stuff works only to set relations, if you try to call order.CustomersReference.Load() to load related customer you will get exception, if you call it before the order object is attached to the context you get:
- The EntityReference could not be loaded because it is not attached to an ObjectContext. *
This is the expected behavior, after all if the order is still transient it cannot load anything, but if you attach the order with AddToOrders() before calling the Load method you will get another exception
- The source query for this EntityCollection or EntityReference cannot be returned when the related object is in either an added state or a detached state and was not originally retrieved using the NoTracking merge option. *
This is annoying because I have a valid order object, it is attached to the context so the RelationshipManager should really loads the Customer Entity for me. It turns out that loading from an entity that is in state addedis not a safe stuff, here is correct snippet
|
|
Now after the order object was committed and is not anymore in added state you can load related customer.
Another interesting operation you can do with an entity key is retrieving an entity given its key.
|
|
This code is really verbose, but it is clearer than retrieving the object with a LINQ query, after all I should have a good way to load an object by key, not using a query with First(). It turns out that you could create an extension method to make your life simpler.
|
|
This extension method will apply to all objectcontext objects, it needs caller to specify type of the entity to load, name of the key property and value to retrieve key property. It works only with entities with single value key, but I left to the reader the task to extend it to supply multiple valued keys. Now you can load a Customer by Key with this code
|
|
I wonder why such a method was not included in the base definition of the objectContext.
Alk.