Generate POCO for Database Tables Using T4 Template
Posted on Mon 12 May 2014 in Developer • Tagged with poco, t4, dotNET, C# • Leave a comment
If you are using Micro-ORM which doesn’t have fancy UI that generate your data model for you, or you want a free tool to generate POCO classes for you, I’ve got a solution for you. Personally I prefer using T4 template that PetaPoco provides, so let’s see how you can use it.
Let’s Start:
First, install/add PetaPoco to your project using Nuget:
1. Open **Package Manager Console** and type `install-package petapoco`
2. Wait until package downloaded and installed to your project.
Second, modify T4 template to generate POCO classes for you:
- Open
Database.tt
file and modify Settings part. - In
ConnectionStringName
write your database connection string name which located in app.config or web.config. - In
Namespace
write the namespace for generated Poco classes. - In
RepoName
write Repository name, this is optional and recommended if you’ll use PetaPoco. - In
ClassPerfex
write required class name prefix, this is optional. - In
ClassSuffix
write required class name suffix, this is optional. - Save the file.
Third, congratulation your POCO classes and Repo generated under Database.tt –> Database.cs.
Notes:
- Make sure you set
providerName
inConnectString
or T4 template will not work. - If you faced C# issue during …