Skip to content

Instantly share code, notes, and snippets.

@maxkoryukov
Created April 24, 2016 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxkoryukov/3454b349a7619c77460f485b566850de to your computer and use it in GitHub Desktop.
Save maxkoryukov/3454b349a7619c77460f485b566850de to your computer and use it in GitHub Desktop.
Simple .dbml to .sql converter (XSLT)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="2.0"
xmlns:dbml="http://schemas.microsoft.com/linqtosql/dbml/2007"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
<xsl:variable name="newline" select="'&#xa;'"/>
<xsl:template match="dbml:Database">
<xsl:for-each select="dbml:Table">
<xsl:text>CREATE TABLE </xsl:text>
<xsl:value-of select="@Name" />
<xsl:text> (</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:for-each select="dbml:Type/dbml:Column">
<xsl:text> [</xsl:text>
<xsl:value-of select="@Name" />
<xsl:text>] </xsl:text>
<xsl:value-of select="@DbType" />
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
<xsl:value-of select="$newline"/>
</xsl:if>
</xsl:for-each>
)
GO
ALTER TABLE <xsl:value-of select="@Name" />
ADD CONSTRAINT PK_<xsl:value-of select="@Name" /> PRIMARY KEY (
<xsl:for-each select="dbml:Type/dbml:Column">
<xsl:if test="@IsPrimaryKey='true'">
<xsl:value-of select="@Name" />
</xsl:if>
</xsl:for-each>
)
GO
<xsl:value-of select="$newline"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment