Um blog sobre nada

Um conjunto de inutilidades que podem vir a ser úteis

Entity Framework–how does Model First generate the database?

Posted by Diego em Julho 7, 2012


On the Model First approach, the database is created from your model. So basically you have your Entity Data Model (EDMX) which is represented by the Entity Data Model Designer (where you create your Entities and Associations) and Visual Studio generate the T-SQL script for you. But how does it do it? How do we go from an EDMX file to a .sql file with the DDL script for all objects?

First of all is good to say that Entity Framework uses the following 4 files, which I’ll refer only by name so forth:

· Entity Data Model (EDMX),

· Conceptual schema definition language (CSDL) – http://msdn.microsoft.com/en-us/library/bb399292.aspx

· Store schema definition language (SSDL)- http://msdn.microsoft.com/en-us/library/bb399559.aspx

· Mapping specification language (MSL) – http://msdn.microsoft.com/en-us/library/bb399202.aspx

 

From msdn:

The Entity Data Model Designer (Entity Designer) stores model and mapping information in an .edmx file at design time. At build time the Entity Designer uses the information in an .edmx file to create the .csdl, .ssdl, and .msl files that are needed by the Entity Framework at runtime.

Basically, it does by following a workflow, which is defined on the “Database Generation Workflow” property of the .edmx file.

The default value is TablePerTypeStrategy.xaml and the file is located on the path: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen

The file looks like this when open on VS:

clip_image002

As you can see, there are 2 activities, “CsdlToSsdlAndMslActivity” and “SsdlToDdlActivity” and what they do is pretty much explained on their names.

The first one (CsdlToSsdlAndMslActivity) reads the CSDL file and generates the SSDL and the MSL file that connect the two.

 

The second (SsdlToDdlActivity) generates DDL from the SSDL file by using a T4 template.
The template is stored in the same folder as the .xaml file and both of them can be graphically set on the Entity Data Model Designer:

clip_image004

What it basically does is provide a script that transforms SSDL into DLL.

Here is an example on how the tables are created:

clip_image006

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão / Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão / Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão / Alterar )

Google+ photo

Está a comentar usando a sua conta Google+ Terminar Sessão / Alterar )

Connecting to %s

 
%d bloggers like this: