Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Created May 26, 2010 09:47
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 carlhoerberg/414291 to your computer and use it in GitHub Desktop.
Save carlhoerberg/414291 to your computer and use it in GitHub Desktop.
Work with Linq to Sql and Migrator
@echo off
setlocal
set path="%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\10.0";"%ProgramFiles(x86)%\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools";%path%
set connStr="Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=true"
REM Download MigratorDotNet from http://code.google.com/p/migratordotnet/
echo Applying migrations
..\Libs\Migrator\Migrator.Console SqlServer %connStr% "..\MyProject.Migrations\Bin\Debug\MyProject.Migrations.dll" -trace
REM If you only have Windows SDK 6 then change the path in top to "%programfiles%\Microsoft SDKs\Windows\v6.0A\Bin"
echo Regenerating DBML
sqlmetal /conn:%connStr% /dbml:MyDB.dbml /namespace:MyProject.Data.Entities /context:MyDBDataContext /functions
REM Download msxsl.exe from http://www.microsoft.com/downloads/details.aspx?familyid=2fb55371-c94e-4373-b0e9-db4816552e41&displaylang=en
echo Applying XSLT to DBML and copying to destination
msxsl MyDB.dbml MyDB.xslt -o ..\MyProject.Data\Entities\MyDB.dbml
REM Download L2ST4 from http://l2st4.codeplex.com/
REM If having trouble with TextTransform, change path in top to to "%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\1.2"
echo Transforming L2ST4 template
TextTransform ..\MyProject.Data\Entities\MyDB.tt
echo Done
endlocal
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:dbml="http://schemas.microsoft.com/linqtosql/dbml/2007"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:script-items"
exclude-result-prefixes="msxsl dbml user">
<xsl:output method="xml" indent="yes"/>
<!-- By default copy all nodes an attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- You probably don't want an Entity for the SchemaInfo table which Migrator will add
This is how you hide a table
-->
<xsl:template match="//dbml:Table[@Name='dbo.SchemaInfo']"/>
<!-- Replace a column with a Enum.
The column in the MyTable table is called TypeEnum.
The Enum defined in my project is called "MyEnum".
I replace the name "TypeEnum" with "Type"
-->
<xsl:template match="//dbml:Type[@Name = 'MyTable']/dbml:Column[@Name = 'TypeEnum']">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:attribute name="Type">MyEnum</xsl:attribute>
<xsl:attribute name="Member">Type</xsl:attribute>
</xsl:copy>
</xsl:template>
<!-- SqlMetal has a bug
(http://connect.microsoft.com/VisualStudio/feedback/details/486598/sqlmetal-generates-incorrectly-capitalized-identifiers)
which this code will resolve, only needed if you have non ascii characters in you table or column names.
But then you need a better MSXSL.exe: http://www.codeproject.com/KB/XML/msxslplus.aspx
-->
<!--
<xsl:template match="@Name">
<xsl:attribute name="Name">
<xsl:value-of select="user:FixCasing(.)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@Member">
<xsl:attribute name="Member">
<xsl:value-of select="user:FixCasing(.)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@Type">
<xsl:attribute name="Type">
<xsl:value-of select="user:FixCasing(.)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@ThisKey">
<xsl:attribute name="ThisKey">
<xsl:value-of select="user:FixCasing(.)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@OtherKey">
<xsl:attribute name="OtherKey">
<xsl:value-of select="user:FixCasing(.)"/>
</xsl:attribute>
</xsl:template>
<msxsl:script language="C#" implements-prefix="user">
<![CDATA[
public string FixCasing(string str)
{
char[] chars = str.ToCharArray();
for (int i = 0; i < chars.Length - 1; i++)
// Add your troubling characters
if ((chars[i] == 'å' || chars[i] == 'ä' || chars[i] == 'ö' || chars[i] == 'Å' || chars[i] == 'Ä' || chars[i] == 'Ö') && char.IsLetter(chars[i + 1]))
chars[i + 1] = char.ToLower(chars[i + 1]);
return new string(chars);
}
]]>
</msxsl:script>
-->
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment