DCSIMG
Model First 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

Model First in Entity Framework 4

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

Comments

DotNetKicks.com said:

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

# May 23, 2009 1:18 PM

Dew Drop - May 23, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - May 23, 2009 | Alvin Ashcraft's Morning Dew

# May 24, 2009 5:17 AM

Dennis said:

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?

# July 21, 2009 1:46 PM

Gunnar Peipman's ASP.NET blog said:

Entity Framework 4.0 is able to generate database schema based on model. If you built your model first

# July 23, 2009 7:44 PM

Entity Framework 4.0: Generating SQL script from model | rapid-DEV.net said:

Pingback from  Entity Framework 4.0: Generating SQL script from model | rapid-DEV.net

# July 23, 2009 8:41 PM

Entity Framework 4.0: Generating SQL script from model | I love .NET! said:

Pingback from  Entity Framework 4.0: Generating SQL script from model | I love .NET!

# July 23, 2009 9:33 PM

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:22 PM

James Manning » Blog Archive » What’s pinned to your Win7 taskbar? said:

Pingback from  James Manning  » Blog Archive   » What’s pinned to your Win7 taskbar?

# November 14, 2009 4:27 AM

Tecnocrata said:

Complementando el post de Luis Franco ( geeks.ms/.../mejoras-en-entity

# June 13, 2010 11:25 PM

Fixing Entity Framework Model-First SQL Express Errors | Gabe Sumner said:

Pingback from  Fixing Entity Framework Model-First SQL Express Errors | Gabe Sumner

# February 13, 2012 4:04 AM

Migrazione a Visual Studio 2010 B1 e al Framework 4: ahi ahi che dolor | Solution architect said:

Pingback from  Migrazione a Visual Studio 2010 B1 e al Framework 4: ahi ahi che dolor | Solution architect

# March 26, 2013 5:43 PM

Entity Framework 4.0: Generating SQL script from model | Gunnar Peipman - Programming Blog said:

Pingback from  Entity Framework 4.0: Generating SQL script from model   | Gunnar Peipman -  Programming Blog

# May 21, 2013 6:37 PM