Skip to content

Commit 7a05062

Browse files
committed
#712 Added Settings.TrimCharFields option to TrimEnd() of 'char' fields. Thanks to Peterhup.
1 parent ca218e7 commit 7a05062

File tree

11 files changed

+77
-2
lines changed

11 files changed

+77
-2
lines changed

BuildTT/BuildTT.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private static void CreateTT(string generatorRoot, string ttRoot)
108108
Settings.IncludeQueryTraceOn9481Flag = false; // If SqlServer 2014 appears frozen / take a long time when this file is saved, try setting this to true (you will also need elevated privileges).
109109
Settings.UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
110110
Settings.IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code
111+
Settings.TrimCharFields = false; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
111112
Settings.AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. new List<string> { ""Microsoft.AspNetCore.Identity.EntityFrameworkCore"", ""System.ComponentModel.DataAnnotations"" };
112113
Settings.AdditionalContextInterfaceItems = new List<string>(); // example: new List<string> { ""void SetAutoDetectChangesEnabled(bool flag);"" };
113114
Settings.AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry

EntityFramework.Reverse.POCO.Generator/Database NorthwindSqlCe40.tt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
Settings.AddParameterlessConstructorToDbContext = true; // EF6 only. If true, then DbContext will have a default (parameter-less) constructor which automatically passes in the connection string name, if false then no parameter-less constructor will be created.
7070
Settings.ConfigurationClassName = "Configuration"; // Configuration, Mapping, Map, etc. This is appended to the Poco class name to configure the mappings.
7171
Settings.DatabaseReaderPlugin = ""; // Eg, "c:\\Path\\YourDatabaseReader.dll,Full.Name.Of.Class.Including.Namespace". See #501. This will allow you to specify a pluggable provider for reading your database.
72+
Settings.UseMappingTables = false; // Can only be true for TemplateType.Ef6 & TemplateType.EfCore5. If true, mapping will be used and no mapping tables will be generated. If false, all tables will be generated.
7273

7374
Settings.EntityClassesModifiers = "public"; // "public partial";
7475
Settings.ConfigurationClassesModifiers = "public"; // "public partial";
@@ -91,6 +92,7 @@
9192
Settings.IncludeQueryTraceOn9481Flag = false; // If SqlServer 2014 appears frozen / take a long time when this file is saved, try setting this to true (you will also need elevated privileges).
9293
Settings.UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
9394
Settings.IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code
95+
Settings.TrimCharFields = false; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
9496
Settings.AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. new List<string> { "Microsoft.AspNetCore.Identity.EntityFrameworkCore", "System.ComponentModel.DataAnnotations" };
9597
Settings.AdditionalContextInterfaceItems = new List<string>(); // example: new List<string> { "void SetAutoDetectChangesEnabled(bool flag);" };
9698
Settings.AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry
@@ -347,6 +349,16 @@
347349
}
348350
};
349351

352+
// Writes any boilerplate stuff inside the POCO class body
353+
Settings.WriteInsideClassBody = delegate(Table t)
354+
{
355+
// Example:
356+
//return " // " + t.NameHumanCase + Environment.NewLine;
357+
358+
// Do nothing by default
359+
return string.Empty;
360+
};
361+
350362
// Using Views *****************************************************************************************************************
351363
// SQL Server does not support the declaration of primary-keys in VIEWs. Entity Framework's EDMX designer (and this generator)
352364
// assume that all non-null columns in a VIEW are primary-key columns, this will be incorrect for most non-trivial applications.
@@ -615,4 +627,4 @@
615627
generator.ReadDatabase();
616628
generator.GenerateCode();
617629
}
618-
fileManagement.Process(true);#>
630+
fileManagement.Process(true);#>

EntityFramework.Reverse.POCO.Generator/Database.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
Settings.IncludeQueryTraceOn9481Flag = false; // If SqlServer 2014 appears frozen / take a long time when this file is saved, try setting this to true (you will also need elevated privileges).
9393
Settings.UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
9494
Settings.IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code
95+
Settings.TrimCharFields = false; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
9596
Settings.AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. new List<string> { "Microsoft.AspNetCore.Identity.EntityFrameworkCore", "System.ComponentModel.DataAnnotations" };
9697
Settings.AdditionalContextInterfaceItems = new List<string>(); // example: new List<string> { "void SetAutoDetectChangesEnabled(bool flag);" };
9798
Settings.AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
public static bool IncludeQueryTraceOn9481Flag = false; // If SqlServer 2014 appears frozen / take a long time when this file is saved, try setting this to true (you will also need elevated privileges).
8787
public static bool UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
8888
public static bool IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code
89+
public static bool TrimCharFields = false; // EF Core option only. EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
8990
public static List<string> AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. "Microsoft.AspNet.Identity.EntityFramework"
9091
public static List<string> AdditionalContextInterfaceItems = new List<string>(); // example: "void SetAutoDetectChangesEnabled(bool flag);"
9192
public static List<string> AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry
@@ -5776,6 +5777,11 @@
57765777
else if (c.Precision > 0 && DatabaseReader.IsPrecisionType(c.SqlPropertyType))
57775778
sb.AppendFormat(".HasPrecision({0})", c.Precision);
57785779
}
5780+
5781+
if (Settings.TrimCharFields && c.MaxLength > 1 && c.SqlPropertyType == "char")
5782+
{
5783+
sb.Append(".HasConversion(new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd()))");
5784+
}
57795785
}
57805786

57815787
sb.Append(c.IsNullable ? ".IsRequired(false)" : ".IsRequired()");
@@ -18815,6 +18821,9 @@ using {{this}};{{#newline}}
1881518821
if (Settings.IncludeCodeGeneratedAttribute)
1881618822
usings.Add("System.CodeDom.Compiler");
1881718823

18824+
if (Settings.TrimCharFields)
18825+
usings.Add("Microsoft.EntityFrameworkCore.Storage.ValueConversion");
18826+
1881818827
return usings;
1881918828
}
1882018829

@@ -20271,6 +20280,9 @@ using {{this}};{{#newline}}
2027120280
if (Settings.IncludeCodeGeneratedAttribute)
2027220281
usings.Add("System.CodeDom.Compiler");
2027320282

20283+
if (Settings.TrimCharFields)
20284+
usings.Add("Microsoft.EntityFrameworkCore.Storage.ValueConversion");
20285+
2027420286
return usings;
2027520287
}
2027620288

@@ -21704,6 +21716,9 @@ using {{this}};{{#newline}}
2170421716
if (Settings.IncludeCodeGeneratedAttribute)
2170521717
usings.Add("System.CodeDom.Compiler");
2170621718

21719+
if (Settings.TrimCharFields)
21720+
usings.Add("Microsoft.EntityFrameworkCore.Storage.ValueConversion");
21721+
2170721722
return usings;
2170821723
}
2170921724

Generator/Generators/GeneratorEfCore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ protected override void SetupConfig(Column c)
121121
else if (c.Precision > 0 && DatabaseReader.IsPrecisionType(c.SqlPropertyType))
122122
sb.AppendFormat(".HasPrecision({0})", c.Precision);
123123
}
124+
125+
if (Settings.TrimCharFields && c.MaxLength > 1 && c.SqlPropertyType == "char")
126+
{
127+
sb.Append(".HasConversion(new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd()))");
128+
}
124129
}
125130

126131
sb.Append(c.IsNullable ? ".IsRequired(false)" : ".IsRequired()");

Generator/Settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public static class Settings
6060
public static bool IncludeQueryTraceOn9481Flag = false; // If SqlServer 2014 appears frozen / take a long time when this file is saved, try setting this to true (you will also need elevated privileges).
6161
public static bool UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
6262
public static bool IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code
63+
public static bool TrimCharFields = false; // EF Core option only. EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
6364
public static List<string> AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. "Microsoft.AspNet.Identity.EntityFramework"
6465
public static List<string> AdditionalContextInterfaceItems = new List<string>(); // example: "void SetAutoDetectChangesEnabled(bool flag);"
6566
public static List<string> AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry

Generator/Templates/TemplateEfCore2.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,9 @@ public override List<string> PocoConfigurationUsings(PocoConfigurationModel data
12371237
if (Settings.IncludeCodeGeneratedAttribute)
12381238
usings.Add("System.CodeDom.Compiler");
12391239

1240+
if (Settings.TrimCharFields)
1241+
usings.Add("Microsoft.EntityFrameworkCore.Storage.ValueConversion");
1242+
12401243
return usings;
12411244
}
12421245

Generator/Templates/TemplateEfCore3.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,9 @@ public override List<string> PocoConfigurationUsings(PocoConfigurationModel data
13431343
if (Settings.IncludeCodeGeneratedAttribute)
13441344
usings.Add("System.CodeDom.Compiler");
13451345

1346+
if (Settings.TrimCharFields)
1347+
usings.Add("Microsoft.EntityFrameworkCore.Storage.ValueConversion");
1348+
13461349
return usings;
13471350
}
13481351

Generator/Templates/TemplateEfCore5.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,9 @@ public override List<string> PocoConfigurationUsings(PocoConfigurationModel data
13201320
if (Settings.IncludeCodeGeneratedAttribute)
13211321
usings.Add("System.CodeDom.Compiler");
13221322

1323+
if (Settings.TrimCharFields)
1324+
usings.Add("Microsoft.EntityFrameworkCore.Storage.ValueConversion");
1325+
13231326
return usings;
13241327
}
13251328

Tester.Integration.EfCore3/Fred.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Microsoft.EntityFrameworkCore.Metadata.Builders;
2727
using Microsoft.EntityFrameworkCore.Query;
2828
using Microsoft.EntityFrameworkCore.Query.Internal;
29+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
2930
using System;
3031
using System.Collections;
3132
using System.Collections.Generic;
@@ -71,6 +72,7 @@ public interface IFredDbContext : IDisposable
7172
DbSet<BringTheAction> BringTheActions { get; set; } // BringTheAction
7273
DbSet<Burak1> Burak1 { get; set; } // Burak1
7374
DbSet<Burak2> Burak2 { get; set; } // Burak2
75+
DbSet<CalculatedColumnNotNull> CalculatedColumnNotNulls { get; set; } // CalculatedColumnNotNull
7476
DbSet<Car> Cars { get; set; } // Car
7577
DbSet<CarToColour> CarToColours { get; set; } // CarToColour
7678
DbSet<ClientCreationState> ClientCreationStates { get; set; } // ClientCreationState
@@ -374,6 +376,7 @@ public FredDbContext(DbContextOptions<FredDbContext> options)
374376
public DbSet<BringTheAction> BringTheActions { get; set; } // BringTheAction
375377
public DbSet<Burak1> Burak1 { get; set; } // Burak1
376378
public DbSet<Burak2> Burak2 { get; set; } // Burak2
379+
public DbSet<CalculatedColumnNotNull> CalculatedColumnNotNulls { get; set; } // CalculatedColumnNotNull
377380
public DbSet<Car> Cars { get; set; } // Car
378381
public DbSet<CarToColour> CarToColours { get; set; } // CarToColour
379382
public DbSet<ClientCreationState> ClientCreationStates { get; set; } // ClientCreationState
@@ -497,6 +500,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
497500
modelBuilder.ApplyConfiguration(new BringTheActionConfiguration());
498501
modelBuilder.ApplyConfiguration(new Burak1Configuration());
499502
modelBuilder.ApplyConfiguration(new Burak2Configuration());
503+
modelBuilder.ApplyConfiguration(new CalculatedColumnNotNullConfiguration());
500504
modelBuilder.ApplyConfiguration(new CarConfiguration());
501505
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());
502506
modelBuilder.ApplyConfiguration(new ClientCreationStateConfiguration());
@@ -1601,6 +1605,7 @@ public class FakeFredDbContext : IFredDbContext
16011605
public DbSet<BringTheAction> BringTheActions { get; set; } // BringTheAction
16021606
public DbSet<Burak1> Burak1 { get; set; } // Burak1
16031607
public DbSet<Burak2> Burak2 { get; set; } // Burak2
1608+
public DbSet<CalculatedColumnNotNull> CalculatedColumnNotNulls { get; set; } // CalculatedColumnNotNull
16041609
public DbSet<Car> Cars { get; set; } // Car
16051610
public DbSet<CarToColour> CarToColours { get; set; } // CarToColour
16061611
public DbSet<ClientCreationState> ClientCreationStates { get; set; } // ClientCreationState
@@ -1700,6 +1705,7 @@ public FakeFredDbContext()
17001705
BringTheActions = new FakeDbSet<BringTheAction>("C36");
17011706
Burak1 = new FakeDbSet<Burak1>("Id");
17021707
Burak2 = new FakeDbSet<Burak2>("Id");
1708+
CalculatedColumnNotNulls = new FakeDbSet<CalculatedColumnNotNull>("Id");
17031709
Cars = new FakeDbSet<Car>("Id");
17041710
CarToColours = new FakeDbSet<CarToColour>("CarId", "ColourId");
17051711
ClientCreationStates = new FakeDbSet<ClientCreationState>("Id");
@@ -3365,6 +3371,15 @@ public class Burak2
33653371
public virtual Burak1 Burak1_IdT { get; set; } // Burak1.FK_Burak_Test1
33663372
}
33673373

3374+
// CalculatedColumnNotNull
3375+
public class CalculatedColumnNotNull
3376+
{
3377+
public int Id { get; set; } // ID (Primary key)
3378+
public byte Type { get; set; } // Type
3379+
public bool IsCalendar { get; private set; } // IsCalendar
3380+
public bool IsUtilization { get; private set; } // IsUtilization
3381+
}
3382+
33683383
// Car
33693384
public class Car
33703385
{
@@ -4976,6 +4991,21 @@ public void Configure(EntityTypeBuilder<Burak2> builder)
49764991
}
49774992
}
49784993

4994+
// CalculatedColumnNotNull
4995+
public class CalculatedColumnNotNullConfiguration : IEntityTypeConfiguration<CalculatedColumnNotNull>
4996+
{
4997+
public void Configure(EntityTypeBuilder<CalculatedColumnNotNull> builder)
4998+
{
4999+
builder.ToTable("CalculatedColumnNotNull", "dbo");
5000+
builder.HasKey(x => x.Id).HasName("PK_CalculatedColumnNotNull");
5001+
5002+
builder.Property(x => x.Id).HasColumnName(@"ID").HasColumnType("int").IsRequired().ValueGeneratedOnAdd().UseIdentityColumn();
5003+
builder.Property(x => x.Type).HasColumnName(@"Type").HasColumnType("tinyint").IsRequired();
5004+
builder.Property(x => x.IsCalendar).HasColumnName(@"IsCalendar").HasColumnType("bit").IsRequired().ValueGeneratedOnAddOrUpdate();
5005+
builder.Property(x => x.IsUtilization).HasColumnName(@"IsUtilization").HasColumnType("bit").IsRequired().ValueGeneratedOnAddOrUpdate();
5006+
}
5007+
}
5008+
49795009
// Car
49805010
public class CarConfiguration : IEntityTypeConfiguration<Car>
49815011
{
@@ -5266,7 +5296,7 @@ public void Configure(EntityTypeBuilder<dcg_RovColumnDefinition> builder)
52665296
builder.Property(x => x.DomainCatalog).HasColumnName(@"DOMAIN_CATALOG").HasColumnType("nvarchar(128)").IsRequired(false).HasMaxLength(128);
52675297
builder.Property(x => x.DomainSchema).HasColumnName(@"DOMAIN_SCHEMA").HasColumnType("nvarchar(128)").IsRequired(false).HasMaxLength(128);
52685298
builder.Property(x => x.DomainName).HasColumnName(@"DOMAIN_NAME").HasColumnType("nvarchar(128)").IsRequired(false).HasMaxLength(128);
5269-
builder.Property(x => x.Type).HasColumnName(@"TYPE").HasColumnType("char(2)").IsRequired(false).IsFixedLength().IsUnicode(false).HasMaxLength(2);
5299+
builder.Property(x => x.Type).HasColumnName(@"TYPE").HasColumnType("char(2)").HasConversion(new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd())).IsRequired(false).IsFixedLength().IsUnicode(false).HasMaxLength(2);
52705300
}
52715301
}
52725302

0 commit comments

Comments
 (0)