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
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:
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:
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:
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:
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.
Give that property a meaningful name and in it’s properties menu attach
it to the new created complex type’s type.
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.
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:
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.