How to Extract CSDL from EDMX

Published 24 January 08 09:59 AM | dpblogs 

 

The ADO.NET Entity Designer stores all its information in the EDMX file which merely encapsulates the EF metadata artifacts (CSDL/MSL/SSDL content) along with a bunch of designer data in a single file. I’ve described the structure of the EDMX file in a previous post.

The ADO.NET Entity Designer extracts the CSDL from the EDMX file and generates data classes from it. Assuming there are no errors during code generation, these classes are immediately visible in the project and are ready for consumption in the project.

Extracting CSDL from the EDMX is pretty simple (thanks to XLinq) and since numerous customers have asked me for it, here’s a simple code snippet to do that:

private XElement ExtractCsdlContent(string edmxFile)

{

  XElement csdlContent = null;

  XNamespace edmxns = "http://schemas.microsoft.com/ado/2007/06/edmx";

  XNamespace edmns = "http://schemas.microsoft.com/ado/2006/04/edm";

 

  XDocument edmxDoc = XDocument.Load(edmxFile));

  if (edmxDoc != null)

  {

   XElement edmxNode = edmxDoc.Element(edmxns + "Edmx");

   if (edmxNode != null)

   {

     XElement runtimeNode = edmxNode.Element(edmxns + "Runtime");

     if (runtimeNode != null)

     {

      XElement conceptualModelsNode = runtimeNode.Element(edmxns +

        "ConceptualModels");

      if (conceptualModelsNode != null)

      {

        csdlContent = conceptualModelsNode.Element(edmns + "Schema");

      }

     }

   }

  }

  return csdlContent;

}

 

You can use a similar approach to extract the MSL and SSDL content from the EDMX file.

 

Sanjay Nagamangalam
Program Manager, ADO.NET

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# ADO.NET team blog said on January 24, 2008 9:55 PM:

I’ve had this article pending for a while; numerous folks wanted more information on this topic and I

# Mike Taulty's Blog said on January 25, 2008 4:36 AM:

Entity Framework Toolkits & Extras How Does The Entity Designer Generate Code? How To Extract CSDL...

# Marcelo's WebLog said on January 25, 2008 2:05 PM:

I admit it - sometimes I fall in love very hard with some of my tools. Tools that make me productive.

# ADO.NET team blog said on January 25, 2008 4:22 PM:

In previous posts, I’ve described CSDL annotations , how to extract CSDL from EDMX and introduced you

# Soonarpense said on April 1, 2008 8:43 AM:

Good site at you. Much all interesting ..

<a href='http://pizzas.we.bs/california-pizza-kitchen-coupons.html'>pizza</a>

# Soonarpense said on April 1, 2008 8:43 AM:

Good site at you. Much all interesting ..

<a href='http://pizzas.we.bs/california-pizza-kitchen-coupons.html'>pizza</a>

# Mike said on April 9, 2008 1:44 PM:

Small correction....

XDocument edmxDoc = XDocument.Load(edmxFile));

Should only have one closing parenthesis.

Excellent stuff.  Thanks.

# Joseph Marinaccio said on February 17, 2009 4:27 PM:

Thank you, bringing me a little closer to what we are trying to do.

Joseph Marinaccio

Marinaccio Family Design

Leave a Comment

(required) 
(optional)
(required) 
Page view tracker