-
Notifications
You must be signed in to change notification settings - Fork 600
T4 Template Poco Builder
Version 5 only.
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.
Assuming all goes 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.
Some common got-chas
Your chosen DBs ADO.net provider must be registered in the GAC.
gacutil /i Npgsql.dll
Your chosen DBs provider factory must be registered in either the machine config or app/web config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
<remove invariant="Npgsql" />
<add name="PostgreSQL Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe"/>
<remove invariant="System.Data.SqlClient"/>
<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data"/>
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="mysql" connectionString="" providerName="MySql.Data.MySqlClient"/>
<add name="postgres" connectionString="" providerName="Npgsql"/>
<add name="sqlite" connectionString="" providerName="System.Data.SQLite"/>
<add name="mssql" connectionString="" providerName="System.Data.SqlClient"/>
<add name="mssqlce" connectionString="" providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
</configuration>
PetaPoco is proudly maintained by the Collaborating Platypus group and originally the brainchild of Brad Robinson