Partition an entity

Image

Let’s give it a try:

Image

Create the basic model. You already know how to do this, so I won’t repeat the instructions. It should look like this:

Image
Image

Copy the entity. Just select the entire RecipeIngredient entity, press Ctrl+C and then Ctrl+V, or use the menus. The result should look like this:

Image
Image

Rename the duplicate entity and delete unused properties. We’ll call the new entity RecipeComponent. You can rename it in the Properties window or by double-clicking the name on the primary design surface.

We also need to delete the MadeFromRecipe and AmountOfRecipe properties of the original RecipeIngredients entity and the AmountOfIngredient and Unit properties of the new Component entity.

The result should look like this:

Image
Image

Fix up the mappings. We need to do two things in this step. We need to map the properties of the new Component entity, but we also need to make sure that each entity only contains the rows that it represents. We do that by setting a conditional mapping for each entity. Let’s start with that.

Image

We’ll start with the existing RecipeIngredient. We only want this entity to contain ingredients that are not themselves recipes. These ingredients won’t have a value in the MadeFromRecipe field, so we can use an Is Null condition:

Image
Image

Now we’ll do the same thing for the new RecipeComponent entity. First we need to tell the designer which table we want to map to, so choose RecipeIngredient in the <Add a Table or View> cell:

Image

Now add the condition to this mapping. Do the same thing you did for the RecipeIngredient entity, but this time set the MadeFromRecipe column to Not Null:

Image
Image

Set the associations. We still need to clean up the associations between the entities. The original RecipeIngredients entity has an association based on the MadeFromRecipe property, which is no longer applicable to that entity, and the new Component entity has no association to Recipe at all.

Image

Deleting the unwanted association is easy: Just select it on the primary design surface and press the Delete key. You want the association that is 0..1 on the Recipe side. It looks like this:

Image

When you delete the association, the designer will also delete the navigation property from the model. It should look like this:

Image
Image

There is an association tool in the designer toolbox, but I think it’s easier to use the entity context menu because it displays the Add Association dialog, which you’re going to need anyway. So right-click the Recipe entity, and then choose Add, Association... from the context menu.

Image

 

The Add Association dialog will assume a one-to-many relationship, but you’ll need to tell it that you want the association between Recipe and Component (it will default to RecipeIngredient). You’ll also need to uncheck the Add foreign key... checkbox, since there’s already a foreign key in the Component entity. When you have everything set up the way you want it, click OK and you’re done.

Image

Image Take a Break

Whew! Quite a few steps, but only two of them were new. Why don’t you take a break before you complete the review and we move on to the first of the two types of inheritance: Table Per Type?


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset