Extending the generated EDMX file

The generated EDMX file, the file which contains the mappings, entity definitions and mapping target definitions (table, procedure, view) for an Entity Framework project, is extensible using user code regions.

User code regions are regions within the generated file which are marked with a begin marker and an end marker and which contents are preserved across code generation cycles. This in short means that whatever you place between these begin / end markers will be copied to the new version of the EDMX file if you re-generate the code in LLBLGen Pro.

These regions are ideal for extending the generated XML inside the generated EDMX file, for example to add your own function definitions, or additional mappings, meta-data or entity definitions you couldn't add within the LLBLGen Pro designer.

User code regions have a unique name within the file they're placed in and are created by a command in the template used to produce the EDMX file. This means that you can't change the name of the region nor can you add your own in the generated code: if you want to add a new user code region to the EDMX file, you have to add the statement to produce the region to the EDMX template.

See the LLBLGen Pro SDK documentation, the Lpt templates engine, for details about user code region production in templates.

A user code region in an EDMX file looks this:

<!-- __LLBLGENPRO_USER_CODE_REGION_START UserCodeRegionName -->
<!-- __LLBLGENPRO_USER_CODE_REGION_END --> 

The UserCodeRegionName is the name of the user code region, which is unique within the file. The start marker is the entire line the __LLBLGENPRO_USER_CODE_REGION_START token is placed on. The end marker is the entire line the __LLBLGENPRO_USER_CODE_REGION_END marker is placed on.

To add code, in this case XML, to the region, open the EDMX file in a text editor, e.g. drag it onto the LLBLGen Pro designer or open it in the XML editor in VS.NET, and simply place the custom XML you want to add between the two marker lines, like in the following example:

<!-- __LLBLGENPRO_USER_CODE_REGION_START SSDLEntityContainerCustomCode -->
    <AssociationSet Name="FK_Territories_Region" Association="Self.FK_Territories_Region">
        <End Role="Territories" EntitySet="Territories"/>
        <End Role="Region" EntitySet="Region"/>
    </AssociationSet>
<!-- __LLBLGENPRO_USER_CODE_REGION_END -->

Available user code regions in a generated EDMX file

The following user code regions are available and where they're located.

  • SSDLEntityContainerCustomCode. Located at the bottom of the SSDL's EntityContainer definition.
  • SSDLCustomCode. Located at the bottom of the SSDL Schema definition.
  • CSDLEntityContainerCustomCode. Located at the bottom of the CSDL's EnttyContainer definition
  • CSDLCustomCode. Located at the bottom of the CSDL Schema definition
  • MSLEntityContainerMappingCustomCode. Located at the bottom of the MSL entity container mapping definition.
  • MSLCustomCode. Located at the bottom of the MSL Mapping element definition
  • DesignerCustomCode. Located at the bottom of the edmx:designer element definition.