diff --git a/src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj b/src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj index c44a8f0..776f2a1 100644 --- a/src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj +++ b/src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj @@ -3,7 +3,7 @@ net6.0 true - 6.0.2 + 6.0.3 Tony Sneed Tony Sneed Entity Framework Core Scaffolding with Handlebars @@ -12,7 +12,7 @@ https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars icon.png scaffolding reverse-engineer entity-framework-core handlebars - See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.2 + See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.3 latest true true diff --git a/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs b/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs index 5c5cfc5..d324de2 100644 --- a/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs +++ b/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs @@ -506,7 +506,7 @@ private void GenerateKey(IKey key, IEntityType entityType, IndentedStringBuilder KeyDiscoveryConvention.DiscoverKeyProperties( concreteKey.DeclaringEntityType, concreteKey.DeclaringEntityType.GetProperties())) - && UseDataAnnotations || !propertyNameOverriden) + && !propertyNameOverriden) { return; } diff --git a/test/Scaffolding.Handlebars.Tests/Contexts/NorthwindDbContext.cs b/test/Scaffolding.Handlebars.Tests/Contexts/NorthwindDbContext.cs index 1c1f8fe..e541757 100644 --- a/test/Scaffolding.Handlebars.Tests/Contexts/NorthwindDbContext.cs +++ b/test/Scaffolding.Handlebars.Tests/Contexts/NorthwindDbContext.cs @@ -11,6 +11,7 @@ public NorthwindDbContext(DbContextOptions options) : base(options) { } public DbSet Categories { get; set; } public DbSet Products { get; set; } + public DbSet Customers { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -18,6 +19,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .HasComment("A category of products") .Property(category => category.CategoryName) .HasComment("The name of a category"); + modelBuilder.Entity(entity => + { + entity.Property(e => e.CustomerKey).IsFixedLength(); + }); } } } diff --git a/test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs b/test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs index fe56a55..cd7c15a 100644 --- a/test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs +++ b/test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs @@ -16,6 +16,7 @@ namespace FakeNamespace public partial class FakeDbContext : DbContext { public virtual DbSet Categories { get; set; } + public virtual DbSet Customers { get; set; } public virtual DbSet Products { get; set; } public FakeDbContext(DbContextOptions options) : base(options) @@ -45,6 +46,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .HasComment(""The name of a category""); }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.CustomerKey); + + entity.ToTable(""Customer""); + + entity.Property(e => e.CustomerKey) + .HasMaxLength(5) + .IsFixedLength(); + + entity.Property(e => e.City).HasMaxLength(15); + + entity.Property(e => e.CompanyName) + .IsRequired() + .HasMaxLength(40); + + entity.Property(e => e.ContactName).HasMaxLength(30); + + entity.Property(e => e.Country).HasMaxLength(15); + }); + modelBuilder.Entity(entity => { entity.ToTable(""Product""); @@ -87,6 +109,7 @@ namespace FakeNamespace public partial class FakeDbContext : DbContext { public virtual DbSet Category { get; set; } + public virtual DbSet Customer { get; set; } public virtual DbSet Product { get; set; } public FakeDbContext(DbContextOptions options) : base(options) @@ -121,6 +144,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .HasComment(""The name of a category""); }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.CustomerKey); + + entity.ToTable(""Customer""); + + entity.Property(e => e.CustomerKey) + .HasMaxLength(5) + .IsFixedLength(); + + entity.Property(e => e.City).HasMaxLength(15); + + entity.Property(e => e.CompanyName) + .IsRequired() + .HasMaxLength(40); + + entity.Property(e => e.ContactName).HasMaxLength(30); + + entity.Property(e => e.Country).HasMaxLength(15); + }); + modelBuilder.Entity(entity => { entity.HasKey(e => e.ProductIdRenamed); @@ -171,6 +215,7 @@ namespace FakeNamespace public partial class FakeDbContext : DbContext { public virtual DbSet Categories { get; set; } + public virtual DbSet Customers { get; set; } public virtual DbSet Products { get; set; } public FakeDbContext(DbContextOptions options) : base(options) @@ -191,6 +236,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .HasComment(""The name of a category""); }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.CustomerKey); + + entity.ToTable(""Customer""); + + entity.Property(e => e.CustomerKey) + .HasMaxLength(5) + .IsFixedLength(); + + entity.Property(e => e.City).HasMaxLength(15); + + entity.Property(e => e.CompanyName) + .IsRequired() + .HasMaxLength(40); + + entity.Property(e => e.ContactName).HasMaxLength(30); + + entity.Property(e => e.Country).HasMaxLength(15); + }); + modelBuilder.Entity(entity => { entity.ToTable(""Product""); @@ -233,6 +299,7 @@ namespace FakeNamespace public partial class FakeDbContext : DbContext { public virtual DbSet Category { get; set; } + public virtual DbSet Customer { get; set; } public virtual DbSet Product { get; set; } public FakeDbContext(DbContextOptions options) : base(options) @@ -258,6 +325,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .HasComment(""The name of a category""); }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.CustomerKey); + + entity.ToTable(""Customer""); + + entity.Property(e => e.CustomerKey) + .HasMaxLength(5) + .IsFixedLength(); + + entity.Property(e => e.City).HasMaxLength(15); + + entity.Property(e => e.CompanyName) + .IsRequired() + .HasMaxLength(40); + + entity.Property(e => e.ContactName).HasMaxLength(30); + + entity.Property(e => e.Country).HasMaxLength(15); + }); + modelBuilder.Entity(entity => { entity.HasKey(e => e.ProductIdRenamed); @@ -308,6 +396,7 @@ namespace FakeNamespace public partial class FakeDbContext : DbContext { public virtual DbSet Categories { get; set; } + public virtual DbSet Customers { get; set; } public virtual DbSet Products { get; set; } public FakeDbContext(DbContextOptions options) : base(options) @@ -332,6 +421,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(e => e.CategoryName).HasComment(""The name of a category""); }); + modelBuilder.Entity(entity => + { + entity.Property(e => e.CustomerKey).IsFixedLength(); + }); + modelBuilder.Entity(entity => { entity.Property(e => e.RowVersion) @@ -360,6 +454,7 @@ namespace FakeNamespace public partial class FakeDbContext : DbContext { public virtual DbSet Category { get; set; } + public virtual DbSet Customer { get; set; } public virtual DbSet Product { get; set; } public FakeDbContext(DbContextOptions options) : base(options) @@ -384,6 +479,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(e => e.CategoryNameRenamed).HasComment(""The name of a category""); }); + modelBuilder.Entity(entity => + { + entity.Property(e => e.CustomerKey).IsFixedLength(); + }); + modelBuilder.Entity(entity => { entity.Property(e => e.RowVersion) diff --git a/test/Scaffolding.Handlebars.Tests/ExpectedTypeScriptContexts.cs b/test/Scaffolding.Handlebars.Tests/ExpectedTypeScriptContexts.cs index 9b5b3e3..a30d06f 100644 --- a/test/Scaffolding.Handlebars.Tests/ExpectedTypeScriptContexts.cs +++ b/test/Scaffolding.Handlebars.Tests/ExpectedTypeScriptContexts.cs @@ -16,6 +16,7 @@ namespace FakeNamespace public partial class FakeDbContext : DbContext { public virtual DbSet Categories { get; set; } + public virtual DbSet Customers { get; set; } public virtual DbSet Products { get; set; } public FakeDbContext(DbContextOptions options) : base(options) @@ -45,6 +46,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .HasComment(""The name of a category""); }); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.CustomerKey); + + entity.ToTable(""Customer""); + + entity.Property(e => e.CustomerKey) + .HasMaxLength(5) + .IsFixedLength(); + + entity.Property(e => e.City).HasMaxLength(15); + + entity.Property(e => e.CompanyName) + .IsRequired() + .HasMaxLength(40); + + entity.Property(e => e.ContactName).HasMaxLength(30); + + entity.Property(e => e.Country).HasMaxLength(15); + }); + modelBuilder.Entity(entity => { entity.ToTable(""Product""); diff --git a/test/Scaffolding.Handlebars.Tests/HbsCSharpScaffoldingGeneratorTests.cs b/test/Scaffolding.Handlebars.Tests/HbsCSharpScaffoldingGeneratorTests.cs index 01a9663..24dd64b 100644 --- a/test/Scaffolding.Handlebars.Tests/HbsCSharpScaffoldingGeneratorTests.cs +++ b/test/Scaffolding.Handlebars.Tests/HbsCSharpScaffoldingGeneratorTests.cs @@ -526,9 +526,11 @@ public void Save_Should_Write_Entity_Files() // Assert var expectedContextPath = Path.Combine(directory.Path, "Contexts", Constants.Files.CSharpFiles.DbContextFile); var expectedCategoryPath = Path.Combine(directory.Path, "Models", Constants.Files.CSharpFiles.CategoryFile); + var expectedCustomerPath = Path.Combine(directory.Path, "Models", Constants.Files.CSharpFiles.CustomerFile); var expectedProductPath = Path.Combine(directory.Path, "Models", Constants.Files.CSharpFiles.ProductFile); Assert.Equal(expectedCategoryPath, result.AdditionalFiles[0]); - Assert.Equal(expectedProductPath, result.AdditionalFiles[1]); + Assert.Equal(expectedCustomerPath, result.AdditionalFiles[1]); + Assert.Equal(expectedProductPath, result.AdditionalFiles[2]); Assert.False(File.Exists(expectedContextPath)); } @@ -561,10 +563,12 @@ public void Save_Should_Write_Context_and_Entity_Files_With_Prefix() // Assert var expectedContextPath = Path.Combine(directory.Path, "Contexts", $"{filenamePrefix}{Constants.Files.CSharpFiles.DbContextFile}"); var expectedCategoryPath = Path.Combine(directory.Path, "Models", $"{filenamePrefix}{Constants.Files.CSharpFiles.CategoryFile}"); + var expectedCustomerPath = Path.Combine(directory.Path, "Models", $"{filenamePrefix}{Constants.Files.CSharpFiles.CustomerFile}"); var expectedProductPath = Path.Combine(directory.Path, "Models", $"{filenamePrefix}{ Constants.Files.CSharpFiles.ProductFile}"); Assert.Equal(expectedContextPath, result.ContextFile); Assert.Equal(expectedCategoryPath, result.AdditionalFiles[0]); - Assert.Equal(expectedProductPath, result.AdditionalFiles[1]); + Assert.Equal(expectedCustomerPath, result.AdditionalFiles[1]); + Assert.Equal(expectedProductPath, result.AdditionalFiles[2]); } [Fact] @@ -595,10 +599,12 @@ public void Save_Should_Write_Context_and_Entity_Files() // Assert var expectedContextPath = Path.Combine(directory.Path, "Contexts", Constants.Files.CSharpFiles.DbContextFile); var expectedCategoryPath = Path.Combine(directory.Path, "Models", Constants.Files.CSharpFiles.CategoryFile); + var expectedCustomerPath = Path.Combine(directory.Path, "Models", Constants.Files.CSharpFiles.CustomerFile); var expectedProductPath = Path.Combine(directory.Path, "Models", Constants.Files.CSharpFiles.ProductFile); Assert.Equal(expectedContextPath, result.ContextFile); Assert.Equal(expectedCategoryPath, result.AdditionalFiles[0]); - Assert.Equal(expectedProductPath, result.AdditionalFiles[1]); + Assert.Equal(expectedCustomerPath, result.AdditionalFiles[1]); + Assert.Equal(expectedProductPath, result.AdditionalFiles[2]); } private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions revEngOptions, bool useEntityNameMappings) @@ -611,7 +617,7 @@ private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions revEn } private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions revEngOptions, - Action configureOptions,bool useEntityTransformMappings = false, + Action configureOptions, bool useEntityTransformMappings = false, string filenamePrefix = null, bool useAltTemplates = false) { HandlebarsLib.Configuration.NoEscape = true; @@ -709,7 +715,8 @@ private Dictionary GetGeneratedFiles(ScaffoldedModel model, Reve || options == ReverseEngineerOptions.DbContextAndEntities) { generatedFiles.Add(Constants.Files.CSharpFiles.CategoryFile, model.AdditionalFiles[0].Code); - generatedFiles.Add(Constants.Files.CSharpFiles.ProductFile, model.AdditionalFiles[1].Code); + generatedFiles.Add(Constants.Files.CSharpFiles.CustomerFile, model.AdditionalFiles[1].Code); + generatedFiles.Add(Constants.Files.CSharpFiles.ProductFile, model.AdditionalFiles[2].Code); } return generatedFiles; diff --git a/test/Scaffolding.Handlebars.Tests/HbsTypeScriptScaffoldingGeneratorTests.cs b/test/Scaffolding.Handlebars.Tests/HbsTypeScriptScaffoldingGeneratorTests.cs index 703b706..94ee84c 100644 --- a/test/Scaffolding.Handlebars.Tests/HbsTypeScriptScaffoldingGeneratorTests.cs +++ b/test/Scaffolding.Handlebars.Tests/HbsTypeScriptScaffoldingGeneratorTests.cs @@ -275,9 +275,11 @@ public void Save_Should_Write_Entity_Files() // Assert var expectedContextPath = Path.Combine(directory.Path, "Contexts", Constants.Files.TypeScriptFiles.DbContextFile); var expectedCategoryPath = Path.Combine(directory.Path, "Models", Constants.Files.TypeScriptFiles.CategoryFile); + var expectedCustomerPath = Path.Combine(directory.Path, "Models", Constants.Files.TypeScriptFiles.CustomerFile); var expectedProductPath = Path.Combine(directory.Path, "Models", Constants.Files.TypeScriptFiles.ProductFile); Assert.Equal(expectedCategoryPath, result.AdditionalFiles[0]); - Assert.Equal(expectedProductPath, result.AdditionalFiles[1]); + Assert.Equal(expectedCustomerPath, result.AdditionalFiles[1]); + Assert.Equal(expectedProductPath, result.AdditionalFiles[2]); Assert.False(File.Exists(expectedContextPath)); } @@ -310,10 +312,12 @@ public void Save_Should_Write_Context_and_Entity_Files_With_Prefix() // Assert var expectedContextPath = Path.Combine(directory.Path, "Contexts", $"{filenamePrefix}{Constants.Files.TypeScriptFiles.DbContextFile}"); var expectedCategoryPath = Path.Combine(directory.Path, "Models", $"{filenamePrefix}{Constants.Files.TypeScriptFiles.CategoryFile}"); + var expectedCustomerPath = Path.Combine(directory.Path, "Models", $"{filenamePrefix}{Constants.Files.TypeScriptFiles.CustomerFile}"); var expectedProductPath = Path.Combine(directory.Path, "Models", $"{filenamePrefix}{ Constants.Files.TypeScriptFiles.ProductFile}"); Assert.Equal(expectedContextPath, result.ContextFile); Assert.Equal(expectedCategoryPath, result.AdditionalFiles[0]); - Assert.Equal(expectedProductPath, result.AdditionalFiles[1]); + Assert.Equal(expectedCustomerPath, result.AdditionalFiles[1]); + Assert.Equal(expectedProductPath, result.AdditionalFiles[2]); } [Fact] @@ -344,10 +348,12 @@ public void Save_Should_Write_Context_and_Entity_Files() // Assert var expectedContextPath = Path.Combine(directory.Path, "Contexts", Constants.Files.TypeScriptFiles.DbContextFile); var expectedCategoryPath = Path.Combine(directory.Path, "Models", Constants.Files.TypeScriptFiles.CategoryFile); + var expectedCustomerPath = Path.Combine(directory.Path, "Models", Constants.Files.TypeScriptFiles.CustomerFile); var expectedProductPath = Path.Combine(directory.Path, "Models", Constants.Files.TypeScriptFiles.ProductFile); Assert.Equal(expectedContextPath, result.ContextFile); Assert.Equal(expectedCategoryPath, result.AdditionalFiles[0]); - Assert.Equal(expectedProductPath, result.AdditionalFiles[1]); + Assert.Equal(expectedCustomerPath, result.AdditionalFiles[1]); + Assert.Equal(expectedProductPath, result.AdditionalFiles[2]); } private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions options, string filenamePrefix = null) @@ -437,7 +443,8 @@ private Dictionary GetGeneratedFiles(ScaffoldedModel model, Reve || options == ReverseEngineerOptions.DbContextAndEntities) { generatedFiles.Add(Constants.Files.TypeScriptFiles.CategoryFile, model.AdditionalFiles[0].Code); - generatedFiles.Add(Constants.Files.TypeScriptFiles.ProductFile, model.AdditionalFiles[1].Code); + generatedFiles.Add(Constants.Files.TypeScriptFiles.CustomerFile, model.AdditionalFiles[1].Code); + generatedFiles.Add(Constants.Files.TypeScriptFiles.ProductFile, model.AdditionalFiles[2].Code); } return generatedFiles; diff --git a/test/Scaffolding.Handlebars.Tests/Helpers/Constants.cs b/test/Scaffolding.Handlebars.Tests/Helpers/Constants.cs index 7fe81b9..f3aa17b 100644 --- a/test/Scaffolding.Handlebars.Tests/Helpers/Constants.cs +++ b/test/Scaffolding.Handlebars.Tests/Helpers/Constants.cs @@ -48,12 +48,14 @@ public static class CSharpFiles { public const string DbContextFile = Parameters.ContextName + ".cs"; public const string CategoryFile = "Category.cs"; + public const string CustomerFile = "Customer.cs"; public const string ProductFile = "Product.cs"; } public static class TypeScriptFiles { public const string DbContextFile = Parameters.ContextName + ".cs"; public const string CategoryFile = "Category.ts"; + public const string CustomerFile = "Customer.ts"; public const string ProductFile = "Product.ts"; } } diff --git a/test/Scaffolding.Handlebars.Tests/Helpers/HandlebarsTransformers.cs b/test/Scaffolding.Handlebars.Tests/Helpers/HandlebarsTransformers.cs index 9acb43b..c124ac0 100644 --- a/test/Scaffolding.Handlebars.Tests/Helpers/HandlebarsTransformers.cs +++ b/test/Scaffolding.Handlebars.Tests/Helpers/HandlebarsTransformers.cs @@ -8,7 +8,8 @@ public static class HandlebarsTransformers static readonly Dictionary _entityTypeNameMappings = new() { { "Product","ProductRenamed" }, - { "Category","CategoryRenamed" } + { "Customer","CustomerRenamed" }, + { "Category", "CategoryRenamed" } }; static readonly Dictionary _entityPropertyNameMappings = new() { diff --git a/test/Scaffolding.Handlebars.Tests/Models/Customer.cs b/test/Scaffolding.Handlebars.Tests/Models/Customer.cs new file mode 100644 index 0000000..f01a332 --- /dev/null +++ b/test/Scaffolding.Handlebars.Tests/Models/Customer.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Scaffolding.Handlebars.Tests.Models +{ + [Table("Customer")] + public partial class Customer + { + [Key] + [StringLength(5)] + public string CustomerKey { get; set; } + [Required] + [StringLength(40)] + public string CompanyName { get; set; } + [StringLength(30)] + public string ContactName { get; set; } + [StringLength(15)] + public string City { get; set; } + [StringLength(15)] + public string Country { get; set; } + } +} \ No newline at end of file