![]() ![]() Public class EntityMappingContext : DbContext Both associations are unidirectional since we didn't define related navigation properties on Address class as for User and Shipment. As you can see, we've defined BillingAddress as a navigation property on User class and another one on Shipment class named DeliveryAddress. How to Implement a One-to-One Primary Key Association with Code FirstFirst, we start with the POCO classes. Let’s see how we map the primary key associations with Code First. The primary key of one table is also a foreign key of the other. Shared Primary AssociationsAlso know as One-to-One Primary Key Associations, means two related tables share the same primary key values. There are several choices, the first being a One-to-One Primary Key Association. Now let’s create the association mappings for this domain model. In this model we assumed that the billing address of the user is the same as her delivery address. Here is the class diagram for this domain model (note the multiplicities on association lines): Also Shipment has to be delivered to an address so it always has one Delivery Address (Delivery Association). Introducing the Revised ModelIn this revised version, each User could have one BillingAddress (Billing Association). In this case, User and Address classes have a true one-to-one association. If a User has a reference to this instance, as her BillingAddress, the Address instance has to support shared references and needs its own identity. One benefit of this model is the possibility for shared references- another entity class (let’s say Shipment) can also have a reference to a particular Address instance. This is particularly useful for scenarios that we want a dedicated table for Address, so that we can map both User and Address as entities. ![]() In today’s blog post I’m going to discuss how we can address those limitations by changing our mapping strategy. We argued that the relationships between User and Address are best represented with a complex type mapping and we saw that this is usually the simplest way to represent one-to-one relationships but comes with some limitations. NET C# Code First CTP5 Entity Framework In the previous blog post I demonstrated how to map a special kind of one-to-one association-a composition with complex types as the first post in a series about entity association mapping with EF Code First.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |