Model First in Entity Framework 4

May 23, 2009

Model First in Entity Framework 4


One of the new features ofModel First in Entity Framework 4
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
menu item:
Generate Database Script from Model menu item 


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:
Entity Designer Diagram


Step 1
On the EDM Designer surface click the right mouse button and select
Generate Database Script from Model.


Generate Database Script from Model menu item


Step 2
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
database.
Provide a Connction String


Step 3
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.


 


Data Definition Language Generation 


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.


Step 4
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.
Overwrite SSDL and MSL
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
    wizard.

  • 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.


 


Summary


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.


DotNetKicks Image

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

one comment

  1. DennisJuly 21, 2009 ב 13:46

    How to “manually run the generated script
    file in order to create the database.”?

    I’ve been searching for hours now, played around with SV2010 but no luck. I’m using an .mdf-db.

    Can anyone help me?

    Reply