Entity Framework 4.0 – Part3: Complex types

These tutorials are built using Visual Studio 2010 RC and .NET 4.0. RC. This means that Entity Framework 4.0 is used. At the moment of writing this, VS2010 and .NET 4.0 are not yet released as RTM so information provided in this post could change in the future.

A complex type is a set of properties that you group together. Sometimes your code becomes more clear when you group together properties. In this part I’ll show you how you can create complex types from the designer. Before EF4.0, you could also create these complex types. The only downside was that you had to do it manually in the conceptual model.

In the previous post I created a new entity called Person. As you can see below, this Person entity contains all the classic properties.

image

Now I want to group Street, PostalCode and City together in a new complex type, Address. Then I have all the address related properties together. Procedure is as follows:

  • Hold CTRL and select the properties in the model that you want to group together
  • Click on the right mouse button and in the context menu select “Refactor into New Complex Type”

image

Now you’ll see that the model browser comes up and adds a new Complex Type. Now let’s give a more meaningful name. If you open the complex type, you’ll see the properties that we previously selected.

image image

If you look at your model, you’ll see that a ComplexProperty has been added to the list. If you look at the properties, you will see that the type of this ComplexProperty is actual the Address complex type that we just created.

I’ll rename this to Address as well, it will make it more clear.

image image

Ok now let’s see how we can use this complex type:

image

kick it on DotNetKicks.com


No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word