Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 indentation in generated C# code

Tags:

c#

t4

When using T4 to generate C# code, I can't get correct identation with TABS scattered around:

public partial class Foo : Base
{
        public int C { get; set; }
        [MappedProperty("A.B[{C}].Foo")]
    public int Foo { get; set; }
}

I'm using a seemingly correctly indented .TT code similar to the following:

public partial class <#= ViewModelName #>
{
    <#  foreach(var property in ViewModelProperties) { #> 
        <# if(property.Mapping != null) { #>
        [MappedProperty("<#= property.Mapping #>")]
        <# } #>
        public <#= property.TypeDeclaration #> <#= property.MemberName #> { get; set; }
    <# } #>
}

This code snippet reflects what I've already tried to do: make control statements and blocks to a single line as much as possible.

like image 607
White hawk Avatar asked Nov 03 '15 14:11

White hawk


People also ask

What is t4 template in Entity Framework?

t4 template is used to scaffold a DbContext class for the database, and the EntityType. t4 template is used to scaffold entity type classes for each table and view in the database.


1 Answers

I like to do it this way and never had any problems.

public partial class <#= ViewModelName #>
{
<#
    foreach(var property in ViewModelProperties) { 
        if(property.Mapping != null) { 
#>
    [MappedProperty("<#= property.Mapping #>")]
<#
        }
#>
    public <#= property.TypeDeclaration #> <#= property.MemberName #> { get; set; }
<#
    }
#>
}
like image 149
AntiHeadshot Avatar answered Sep 21 '22 03:09

AntiHeadshot