Database Reference
In-Depth Information
If you are using an independent association in which the multiplicity of the related entity is one or 0..1, then Entity
Framework requires the entity keys of those references to be set correctly in order to generate the appropriate where
clause of an update or delete statement. In our example, if we had an independent association between Invoice and
Payment, we would need to set the Invoice navigation property to an instance of Invoice with the correct value for the
InvoiceId property. The resulting where clause would include the PaymentId, TimeStamp, and InvoiceId.
when implementing an n-tier architecture with entity Framework, serious consideration should be given to
using the Foreign key association approach for related entities. the independent association approach is difficult to
implement and can make your code quite complex. For a great explanation of these approaches, including their benefits
and drawbacks, check out the following blog post from arthur Vickers, Developer on the entity Framework team:
whats-the-deal-with-mapping-foreign-keys-using-the-entity-framework . Be certain to check out arthur's other
outstanding blog posts on the entity Framework.
Note
If your entity object contains several independent associations, setting all of them can quickly become tedious.
You might find it simpler just to retrieve the instance from the database and mark it for deletion. This makes your code
a little simpler, but when you retrieve the object from the database, Entity Framework will rewrite the query to bring
in all of the relationships that are one or 0..1, unless, of course, you are using the NoTracking context option. If this
Recipe were implementing the Independent Association approach, when we load the Payment entity prior to marking
for deletion, Entity Framework would create an object state entry for the Payment entity and a relationship entry for
the relationship between Payment and Invoice. When we marked the Payment entity for deletion, Entity Framework
would also mark the relationship entry for deletion. Like previously, the resulting where clause would include the
PaymentId, TimeStamp, and InvoiceId.
Another option for deleting entities in independent associations is to eagerly load the related entities and
transport the entire object graph back to the WCF or Web API service for deletion. In this example, we could eagerly
load the related Invoice entity with the Payment entity. If we were to delete the Payment entity, we could send back
the graph containing both entities to the service. But, be forewarned that this approach consumes more network
bandwidth and processing time for serialization, so the cost may outweigh the benefit of more clarity in the code.
9-6. Managing Concurrency When Disconnected
Problem
You want to make sure that changes made on an entity by a WCF client are applied only if the concurrency token has
not changed.
Solution
Let's suppose that you have a model like the one shown in Figure 9-6 .
 
 
Search WWH ::




Custom Search