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