I always felt that T4 is often the most neglected feature of the .net world. I may be wrong here but have not seen many implementations around it. In spite of being really useful and powerful feature within the Visual Studio.

I felt the need to write this blog to share my own experience working with the T4 template and how can we use it effectively to fit in a Web API use case. These days every application has the APIs at the back to power up the data to the UI layer. So taking up the Web Api example would be much more useful.

In general T4 template  is a combination of text blocks and control logic that can generate a text file. The generated could be a web page, or a resource file, Sql script code, or program source code in any language. In this article we will see how to use T4 template to generate the Controllers, Repository, and DBContext file for each entity in our SQL DB. We will be using Ef Core scaffolding to generate the Models first from our DB and then execute our T4 template on that Model classes generated by the Ef Core.

Steps – All the below steps can actually be done just once having all the T4 code in a single T4 file. For understanding purpose I have created these steps.

  • Generate the Model class using EF Core.
  • Generate the DB Context class using T4.
  • Generate Repository class using T4.
  • Generate Controllers using T4.

Prerequisites – Create a sample DB with some tables. Have the Identity column/primary key column names as Id only. This is needed to generate the generic code for GetById, Delete, etc CRUD operations.

  1. Genearting Model classes – Using EF core we can generate model classes from our SQL DB connections. (We could even use T4 template to generate the Model classes bases on each table we have in our DB. In that case we would need to traverse through the tables collection from our DB and then for each table generate a Model class with Table name as class name and the columns as the class members. We would have to write a bit of extra code to map the column data types in SQL to appropriate data types in c#). Since we have the flexibility of using EF Core it is advisable/recommended to use it for generating the Model class from DB. No need of the edmx file in this case, just directly using the EF Core scaffolding command from the .Net CLI will do the needful.

2. Execute/Save the template (.tt) file – The template file go through multiple steps to generate all the required plumbing code. Once the T4 file execution is success we should be able to just build and test our web API with the CRUD operations ready for every entity.

  • Generating the DB Context – to access the DbSet for each table in the DB. Here we first set the initial variables that would be used across our T4 template file. We would use the project name itself as our base namespace for every class concatenated with the folder name each class resides in.


Now the code to generate our DbContext class with Dbset for each table. The connection string is present here within the DbContext file but we can optimize it by getting it from config. At the end we need to save the generated code into a class file within a folder of our choice.

  • Generate the Repository classes – code for each entity which would be doing the actual CRUD calls to the DB through the DbContext files created in the previous step. Here our T4 code needs to loop through each file present within the Models folder and then create appropriate Repository class for each entity.

  • Generate the Controllers – The next logical step would be to generate our controllers for each entity. These controllers would have the CRUD operations API implemented for which we have already generated the Repository code.

At the end we have 2 functions internally being used by the T4 template to save the generated code into a new file within the specified folder and another small method to return the string in Pascal casing basically just lower casing of the 1st letter of the input string.

As mentioned earlier you can include all the above code in 1 single T4 file and that would still work and generate all the files at once.