How to Extract CSDL from EDMX

 

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)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

  XElement csdlContent = null;

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

  XNamespace edmns = "https://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

Comments

  • Anonymous
    January 24, 2008
    I’ve had this article pending for a while; numerous folks wanted more information on this topic and I

  • Anonymous
    January 24, 2008
    Entity Framework Toolkits &amp; Extras How Does The Entity Designer Generate Code? How To Extract CSDL...

  • Anonymous
    January 25, 2008
    I admit it - sometimes I fall in love very hard with some of my tools. Tools that make me productive.

  • Anonymous
    January 25, 2008
    In previous posts, I’ve described CSDL annotations , how to extract CSDL from EDMX and introduced you

  • Anonymous
    April 01, 2008
    The comment has been removed

  • Anonymous
    April 01, 2008
    The comment has been removed

  • Anonymous
    April 09, 2008
    Small correction.... XDocument edmxDoc = XDocument.Load(edmxFile)); Should only have one closing parenthesis. Excellent stuff.  Thanks.

  • Anonymous
    February 17, 2009
    Thank you, bringing me a little closer to what we are trying to do. Joseph Marinaccio Marinaccio Family Design