Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 Template is Generating Extra New Lines on Some PCs

Tags:

While using T4 classes for entity framework there are a couple of developers who generate classes with one extra new line for every line generated. I'm wondering if this is some kind of setting that needs to be changed so that their T4 generated files look like the generated files form other developers. As an example of what I am talking about: (removed specific names but you should be able to see the difference in the number of new lines generated from the same *.tt file.)

(Update: The issue occurs in other T4 Templates as well, not just EF. Both PCs are using TextTemplatingFileGenerator as the T4 custom tool.)

T4 output from my PC:

    public virtual DbSet<GeneratedObject1> GeneratedObject1 { get; set; }     public virtual DbSet<GeneratedObject2> GeneratedObject2 { get; set; }      public virtual int SomeMethod1(Nullable<int> inParameter)     {         var localParameter = inParameter.HasValue ?             new ObjectParameter("SomePropertyName", inParameter) :             new ObjectParameter("SomePropertyName", typeof(int));          return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SomeMethod1", localParameter);     }      public virtual int SomeMethod2(Nullable<int> inParameter)     {         var localParameter = inParameter.HasValue ?             new ObjectParameter("SomePropertyName", inParameter) :             new ObjectParameter("SomePropertyName", typeof(int));          return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SomeMethod2", localParameter);     } 

T4 output from their PC:

public virtual DbSet<GeneratedObject1> GeneratedObject1 { get; set; }  public virtual DbSet<GeneratedObject2> GeneratedObject2 { get; set; }   public virtual int SomeMethod1(Nullable<int> inParameter) {      var localParameter = inParameter.HasValue ?         new ObjectParameter("SomePropertyName", inParameter) :         new ObjectParameter("SomePropertyName", typeof(int));       return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SomeMethod1", localParameter); }   public virtual int SomeMethod2(Nullable<int> inParameter) {      var localParameter = inParameter.HasValue ?         new ObjectParameter("SomePropertyName", inParameter) :         new ObjectParameter("SomePropertyName", typeof(int));       return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SomeMethod2", localParameter); } 

Edit:

(Of roughly the same text in the file.) My file: My file

Their file: Their file

like image 452
Thomas927 Avatar asked Sep 13 '16 16:09

Thomas927


People also ask

How do I debug a T4 template?

To debug a design-time text template, save the text template file, and then choose Debug T4 Template on the shortcut menu of the file in Solution Explorer. To debug a run-time text template, simply debug the application to which it belongs.

What is true about T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.

How do T4 templates work?

In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. In Visual Studio 2015 Update 2 and later, you can use C# version 6.0 features in T4 templates directives.

What is transform all T4 templates?

t4 is basically a tool built into VS for doing text transformation, typically for doing code generation. Transform All T4 Templates searches your solution for *. tt files and executes them to create other text, again typically source code, files.


1 Answers

What @ralf.w. was getting at was the solution to this problem. The line endings in the .tt files on the problem computer were LF and this causes extra line endings to be generated when the transformation tool runs. The correct line endings should be CR LF. Once we changed the line endings in the .tt files, the output files were generated properly. I also changed the line ending settings in Git to checkout as-is, commit as-is. This question has some information on what the line ending settings mean and where they can be changed.

Notepad++ was used to convert the problem .tt files to CR LF (we didn't have that many.) Go to EditEOL ConversionWindows (CR LF)

like image 129
Thomas927 Avatar answered Sep 20 '22 17:09

Thomas927