Tuesday, 30 March 2010

How to use Subsonic 3 T4 templates in VS Express

Subsonic is an ORM tool that works up your database access and can speed your development cycle. It can be downloaded from . To use subsonic in your visual studio express project you can follow the steps below:

1) in your project add a reference to the subsonic.core.dll

2) create a folder Dal – the generated classes will be put in this directory

3) create a folder Model and populate it with the subsonic T4 templates.

4) create a batch file ie. model.bat and add the following:

echo Building Context....
call "C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2
\TextTransform.exe" -out DAL\Context.cs Model\Context.tt
echo Building Classes....
call "C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2
\TextTransform.exe" -out DAL\Classes.cs Model\Classes.tt
echo Building StoredProcedures....
call "C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2
\TextTransform.exe" -out DAL\StoredProcedures.cs
echo Building Structs....
call "C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2
\TextTransform.exe" -out DAL\Structs.cs Model\Structs.tt

5) create a vs external tool with:

Title: s/ing like Subsonic 3 ?
command: %SystemRoot%\system32\cmd.exe
argument: /C "model\model.bat"
Initital Directory: $(ProjectDir)

6) modify the Settings.ttinclude with the connection string of your db and your namespace for the generated code according to the Subsonic documentation.

7) if T4 throws exceptions modify the Settings.ttinclude as explained in this .

Note1: if you want to build each .tt file separately follow the steps below:

a) replace the contents of the model.bat with the following:

call "C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2\TextTransform.exe" -out %1 %2

b) set the argument of the external tool with:

 /C "model\model.bat Dal\$(ItemFileName).cs model\$(ItemFileName)$(ItemExt)"

Note 2: keep the quotes, it’s important!

No comments:

Post a Comment