Thursday, August 4, 2011

Annotating EDMX with meta data from Sql Server

The EDMX files generated by EntityModelCodeGenerator and EdmGen.exe don't load metadata from SQL, such as the "MS_Description" extended property, into the Documentation. As such you get lots of classes generated with "No Metadata Documentation available." summaries.

I'm working with a database where lots of useful metadata has been loaded into the Extended Properties of tables, columns and foreign keys. Loading this data into the EDMX conceptual schema definition language (CSDL) as annotations allows it to be accessed as MetadataPropertes by the EntityClassGenerator/T4 templates to apply data annotation attributes etc... to classes.

An EDMX EntityType with the Documentation set appears as:

  <EntityType Name="EntityName">
    <!-- Without this element classes are typically generated with 
         "No Metadata Documentation available." -->
    <Documentation>
            <Summary>Entity Summary</Summary>
            <LongDescription>Entity Long Description</LongDescription>
          </Documentation>
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="String" Nullable="false" MaxLength="25" Unicode="false" FixedLength="true" />
    <!-- Lots of other properties -->
  </EntityType>

Annotations to appear as MetadataProperties can be added as well.

See Also: