Model First in Entity Framework 4
One of the new features of
EF4 is the ability to generate a
database and the SSDL and MSL that
correspond to a provided model.
In this feature we can start building
our conceptual model first and then generate
all the other parts of the EDM according to it.
In this post I’ll introduce the model first feature.
Generating a Database from a Given Conceptual Model
One thing that was missing (in my opinion) in the first release
of EF was the ability to start from a conceptual model and then
to generate the other parts of the EDM and the database. In the
new Visual Studio 2010 Beta 1 that feature was given. Now we have
a Create Database Wizard to help us generate a database from an
Entity Data Model. We can start in building our model first and then
use that feature to do all the other work. A new menu item – Generate
Database Script from Model was added to the EDM Designer start
the Generate Database Script Wizard. The next figure shows the new
How to Generate a Database from a Given Conceptual Model?
The following steps will help you to understand the process of
generating a database from a given Entity Data Model. In the
example I’m using the following model that was created as an
empty model without the EDM Wizard:
On the EDM Designer surface click the right mouse button and select
Generate Database Script from Model.
In the Generate Database Script Wizard, click the New Connection button
or select an existing connection from the drop-down list. Pay attention that
providing a connection string wouldn’t not initiate the generation of the
After choosing a connection string the next step will be to get the
generated data definition language (DDL) script and to save it in a
proper SQL script file.
You can customize the DDL generated to your database of choice.
Pay attention that the default inheritance mapping strategy is
table per type. This can also be overwritten if you choose to.
Pressing the Finish button will raise a question whether to overwrite
your existing SSDL and MSL. Choosing Yes will overwrite them and
choosing No will keep your previous data.
When choosing the Yes button the following things happen:
- Generate the store schema definition language (SSDL).
- Generate the mapping specification language (MSL).
- Update the .edmx file with the generated SSDL and MSL.
- Saves the generated data definition language (DDL) in the
location you specified in the Save DDL As text box of the
- Add a connection string to your configuration file.
Pay attention that you have to manually run the generated script
file in order to create the database. The wizard process doesn’t do
it for you.
Lets sum up, in today’s post I showed the new model first feature
of EF4. That feature enables us to start with a model and then to
generate its database. The process of generating the database can
be customized and you are able to create your database of choice.