How To: use your existing CSDL/MSL/SSDL files in the Entity Designer CTP2

Sanjay Nagamangalam who is the lead PM for the Entity Designer wrote up this great how-to for taking existing EF metadata files and bootstrapping them into the designer. Thanks Sanjay!

How To: use your existing CSDL/MSL/SSDL files in the Entity Designer CTP2

 

The ADO.NET Entity Designer stores EF metadata artifacts (CSDL/MSL/SSDL content) in a .edmx file along with designer data. The .edmx file basically has CSDL, MSL and SSDL content in separate XML elements. When the project is built, the EntityDeploy MSBUILD task extracts the CSDL/MSL/SSDL content from the .edmx file and places them in the project output directory or embeds them in the output assembly depending on the value of the “Metadata Artifact Processing” property set in the designer.

 

While we officially don’t support it, creating a .edmx file from your existing CSDL/MSL/SSDL files is pretty straight forward. Here's how to do it:

 

Add an Empty Model to your project

1.       Start VS 2008 RTM

2.       Add a new Entity Data Model item to your project

3.       Specify a file name, choose “Empty Model” in the wizard and click Finish

4.       The designer add an empty model to the project and also open the .edmx file

5.       Close the designer and click “Yes” when prompted to save changes

 

Copy/paste contents of your existing CSDL/SSDL/MSL files into the .edmx file

1.        Right-click on the .edmx file in Solution Explorer and choose “Open with… XML Editor”

2.        Replace the highlighted text with the contents of your existing CSDL file

    <!-- CSDL content -->

    <edmx:ConceptualModels>

      <Schema

        xmlns="schemas.microsoft.com/ado/2006/04/edm"

        Namespace="Model1"

        Alias="Self"

      >

        <EntityContainer Name="ModelContainer" />

      </Schema>

    </edmx:ConceptualModels>

 

3.        Replace the highlighted text with the contents of your existing SSDL file

    <!-- SSDL content -->

    <edmx:StorageModels>

      <Schema

        xmlns="schemas.microsoft.com/ado/2006/04/edm/ssdl"

        Namespace="Model1.Target"

        Alias="Self"

      >

        <EntityContainer Name="TargetContainer" />

      </Schema>

    </edmx:StorageModels>

 

4.        Replace the highlighted text with the contents of your existing MSL file

    <!-- C-S mapping content -->

    <edmx:Mappings>

      <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS"

         Space="C-S"

      >

        <Alias Key="Model" Value="Model1"/>

        <Alias Key="Target" Value="Model1.Target"/>

 

        <EntityContainerMapping

          CdmEntityContainer="ModelContainer"

          StorageEntityContainer="TargetContainer">

        </EntityContainerMapping>

      </Mapping>

    </edmx:Mappings>

 

5.        Save and close the .edmx file

 

Fix up diagram layout

1.        Double-click the .edmx file in Solution Explorer to launch the designer

2.        Right-click on an empty area of the designer surface and choose “Diagram…Layout Diagram”

3.        Save and close the .edmx file

 

NOTE: manually edited .edmx files may not display in the designer if they have content not supported by the designer. These include Complex Types, TPC mapping, C-Side conditions, schema references (“using”), query views, defining queries, command text, unmapped abstract types, and multiple entity sets per type. Please refer to the ReadMe for more details.

 

Update App.Config before running your application

1.        Add an App.Config or a Web.Config file to your project if its missing

2.        Add an entry for the Entity Connection string to App.Config or Web.Config in the <connectionStrings> section and replace the highlighted portions as suggested below:

<configuration>

  <connectionStrings>

    <add name="[EntityContainer name from your CSDL file]" connectionString="metadata=.\[filename of .edmx file].csdl|.\[filename of .edmx file].ssdl|.\[filename of .edmx file].msl;provider=System.Data.SqlClient;provider connection string=&quot;[insert your provider connection string here]&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>

</configuration>

 

3.        For example, the provider connection string to connect to the Northwind database on SQL Express on the local computer might look like this:

provider connection string=&quot;Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True&quot;

 

4.        Assuming the EF metadata artifacts are copied to the project output directory, here’s an example of what the full Entity Connection string might look like for Northwind on SQL Express on the local computer and the edmx file is called NorthwindModel.edmx:

<configuration>

  <connectionStrings>

    <add name="NorthwindEntities" connectionString="metadata=.\NorthwindModel.csdl|.\NorthwindModel.ssdl|.\NorthwindModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>

</configuration>

 

5.        Save and close App.Config