Entity Data Model Designer
Entity Data Model Designer
In my previous posts I explained how to get started with the entity framework.
Today I’m going to explain how to use the entity data model designer.
Introduction to EDM Designer
The entity data model designer is a designer which is integrated into
visual studio 2008 environment. In order to have the designer you should
download and install the ADO.Net Entity Framework Tools December 07 CTP.
As mentioned earlier in my previous posts the entity framework and its tools are
in CTP and scheduled to be released with visual studio 2008 SP1.
The next step is to add a new entity data model with the entity data model wizard.
I described how to use the wizard in the entity data model post.
After the edmx file was created lets look at the designer and explain key features.
Database and Wizard
In this Example and in the following posts I’m going to use the current database:
After running the wizard on the database the result is an edmx model:
You can start working with that model as is (one to one mapping) but probably
there are a lot of things that you’ll like to change.
Designer Features Examples
One thing to notice is that the entity names are Employees and Companies.
Lets change it to Employee and Company (more accurate names).
There are two ways to do this. The first, stand on the entity and rename its title
by clicking on the title and rename it. In this way only the name of the entity is changed.
The second, click on the entity and then F4 for the Properties view and rename the entity
name and also its entity set name. In my opinion, The second way if preferable.
Implementing Inheritance in EDM
Lets talk about more interesting things in the designer – inheritance.
As you can see in the diagram there is a property of EmployeeType in Employee.
Employee type can be developer, team leader, project manager and etc.
The way of having a property in a table that indicate an hierarchy of objects is called
Table per Hierarchy inheritance or TPH.
So how do I define every type?
First, I’m going to change the inheritance modifier of the employee to abstract.
The reason is obvious – the employee is a base type for every type of employee.
Changing the inheritance modifier is made by clicking on the employee entity and then
F4 to The Properties view.
Look for the Inheritance Modifier property and change it to abstract.
Then, lets add a Developer entity.
You need to be on the designer surface and click on the right mouse button.
In the menu go to Add and then Entity.
Another way to do this is to drag an entity from the toolbox.
The New Entity screen will be opened. Choose the Employee type as the base type of the
Developer type and edit the entity name to Developer. Press OK and the new entity will be
The next thing to do is to add mapping details for the Developer entity. You do it by
adding a table in the Mapping Details view. Click on the Developer entity and the view
will be opened in the bottom of the designer (or where ever you configured it to be open).
In the Mapping Details view choose the Employee table and the mapping will be added.
After adding the mapping to to Employee table we need to remove the property of
EmployeeType from the employee entity. The reason is that this property is indicating the
type of the object to be created and enabling the user to change this property would make
the model inconsistent. That means that if you change that property in a developer entity to
be project manager the class would have to change also. This behavior can cause a lot of
errors and therefore is restricted.
Removing the property is easy. Just click on the property in the designer and press delete button
or click right mouse button and choose delete.
The last thing to do is to add a condition in the developer entity.
The condition will be on the EmployeeType column.
Developer type has an EmployeeType of Developer.
Add a condition on EmployeeType which you choose in the condition drop down.
Then, edit the condition to equals Developer.
I added a program manager and a team leader entities.
The result should look like the picture.
You can add properties to the entities and to use them in you application.
After you finish, compile the solution and your model is ready.
In the next post I’m going to explain how to work with the generated model
in your application.