DCSIMG
Complex Type Support in the EDM Designer in Entity Framework 4 - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Complex Type Support in the EDM Designer in Entity Framework 4

Complex Type Support in the EDM Designer in Entity Framework 4

Yesterday I installed the new Complex Type Support in the EDM Designer in EF4
Visual Studio 2010 Beta 1. Since
then I’m learning the new features
of .NET 4 and in particularly Entity
Framework 4
(or EF4). In the near
future I’m going to write about the new
features and enhancement made in EF4. In this post I’m revisiting a post
I wrote in the past – Creating Complex Types in Entity Framework – and
show how to use the EDM designer in EF4 to achieve the same
functionality.

What are Complex Types?

As I wrote in the previous post, complex types are a way to encapsulate a
set of entity’s properties inside a structure which isn’t an entity.
You use them to organize properties into structures that make your design
more understandable. For more details about complex types in EF V1 read
my previous post.

Complex Type Support in the EDM Designer

In EF4, complex types creation was made easier. If in EF V1 you needed to edit
the CSDL and MSL manually and after that you couldn’t work with the designer,
in EF4 they are part of the EDM Designer. Moreover, the new and improved
Update Model from Database wouldn’t erase your changes if you’ll decide to
do it manually.

Example

In the example I’m going to build a CourseDetails complex type inside
a Course entity. The following figure is the model before the
complex type changes:
Entity Designer Diagram

Step 1 – Create Complex Type
In the designer click right mouse button and in the Add menu item select the
new Complex Type menu item to create the complex type:
Add New Complex Type

Another way to do it is from the Model Browser to use the new menu
item – Create Complex Type.

Step 2 – Rename Complex Type
In the Model Browser, rename the new created complex type to the
desired name. I rename it to CourseDetails:
Rename Complex Type

Step 3 – Add Properties
Add the relevant properties to the complex type. You can copy & paste
the properties from your entities to the Model Browser or in the Model
Browser
use the Add menu item to add your properties manually. The
following figure shows the Model Browser after the change:
Add Properties to Complex Type

Step 4 – Add Complex Type to Entity
After you have the new complex type now it is the time to add it to your
entity. In the designer use the Add –> Complex Property on the entity
to add a new complex property to the entity.   
Add New Complex Property 
Give that property a meaningful name and in it’s properties menu attach
it to the new created complex type’s type.
After Add New Complex Property

Step 5 – Map the Complex Type’s Properties
The last step in the process of creating a new complex type is to
map the complex type’s properties. In the Mapping Details menu
map every complex type property to the relevant field.
Map Complex Type Properties

Step 6 – Testing
After finishing the previous steps you should test the new complex
type
. The following code shows a small program I wrote to check that
I can add and retrieve the data of the new created complex type:

class Program
    {
        static void Main(string[] args)
        {
            using (var context = new SchoolEntities())
            {
                var course = new Course
                {
                    Department = context.Departments.FirstOrDefault(),
                    Title = "My New Course",
                    CourseDetails = new CourseDetails
                    {
                        Credits = 4,
                        Days = "MF",
                        Location = "Class A",
                        Time = DateTime.Now
                    }
                };
 
                context.AddToCourses(course);
                context.SaveChanges();
 
                var newCourse = (from c in context.Courses
                                 where c.Title.Equals("My New Course")
                                 select c).FirstOrDefault();
 
                Console.WriteLine("{0} {1}", newCourse.Title, newCourse.CourseDetails.Time);
                Console.ReadLine();
            }
        }
    }

and the output of the test run:
Test Output 

Summary

Lets sum up, in today’s post I revisited an older post about EF V1’s complex type
support. I showed the new designer enhancement for complex types in EF4 and
showed an example of how to use it. You can expect more posts about EF4 in the
near future.

DotNetKicks Image

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 22, 2009 10:52 AM

Learning resources for Entity Framework 4.0 new features « Bogdan Brinzarea’s blog said:

Pingback from  Learning resources for Entity Framework 4.0 new features «  Bogdan Brinzarea’s blog

# August 5, 2009 12:25 PM

Gil Fink on .Net said:

The page is an index of all the ADO.NET entity framework 4 posts that I wrote: Complex Type Support in

# February 4, 2010 8:38 AM

Moses' Blog said:

Mapping Conceptual Model Function to Complex Type in Entity Framework 4.0

# April 7, 2010 8:59 PM