-
Notifications
You must be signed in to change notification settings - Fork 600
T4 Template Poco Builder
Writing all those POCO objects can soon get tedious and error prone, so PetaPoco includes a T4 template that can automatically write classes for all the tables in your your SQL Server, SQL Server CE, MySQL, PostgreSQL or Oracle database for you.
Using the T4 template is pretty simple. The git repository includes three files (The NuGet package adds these to your project automatically in the folder \Models\Generated).
These files include:
- PetaPoco.Core.ttinclude - includes all the helper routines for reading the DB schema
- PetaPoco.Generator.ttinclude - the actual template that defines what will be generated
- Database.tt - the template itself that includes various settings and includes the two other ttinclude files.
A typical Database.tt file will like this:
<#@ include file="PetaPoco.Core.ttinclude" #>
<#
// Settings
ConnectionStringName = "jab";
Namespace = ConnectionStringName;
DatabaseName = ConnectionStringName;
string RepoName = DatabaseName + "DB";
bool GenerateOperations = true;
// Load tables
var tables = LoadTables();
#>
<#@ include file="PetaPoco.Generator.ttinclude" #>
To use the template:
- Add the three files to you C# project (or use the nuget package)
- Make sure you have a connection string and provider name set in your app.config or web.config file
- Edit ConnectionStringName property in Records.tt (ie: change it from "jab" to the name of your connection string)
- Save Database.tt.
All going well Database.cs should be generated with POCO objects representing all the tables in your database. To get the project to build you'll also need to add PetaPoco.cs to your project and ensure it is set to compile (NuGet does this for you) .
The template is based on the SubSonic template. If you're familiar with its ActiveRecord templates you'll find PetaPoco's template very similar.
PetaPoco is proudly maintained by the Collaborating Platypus group and originally the brainchild of Brad Robinson