Skip to content

Commit

Permalink
Merge pull request #205 from TrackableEntities/fix-has-key-omission
Browse files Browse the repository at this point in the history
[Fix] HasKey Reference Left Out with Non-Conventional Key Property
  • Loading branch information
tonysneed committed Jan 22, 2022
2 parents 0529b73 + dd6a0a3 commit 2b65b77
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 13 deletions.
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>6.0.2</Version>
<Version>6.0.3</Version>
<Authors>Tony Sneed</Authors>
<Company>Tony Sneed</Company>
<Title>Entity Framework Core Scaffolding with Handlebars</Title>
Expand All @@ -12,7 +12,7 @@
<PackageProjectUrl>https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars</PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageTags>scaffolding reverse-engineer entity-framework-core handlebars</PackageTags>
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.2</PackageReleaseNotes>
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.3</PackageReleaseNotes>
<LangVersion>latest</LangVersion>
<IncludeSource>true</IncludeSource>
<SignAssembly>true</SignAssembly>
Expand Down
Expand Up @@ -506,7 +506,7 @@ private void GenerateKey(IKey key, IEntityType entityType, IndentedStringBuilder
KeyDiscoveryConvention.DiscoverKeyProperties(
concreteKey.DeclaringEntityType,
concreteKey.DeclaringEntityType.GetProperties()))
&& UseDataAnnotations || !propertyNameOverriden)
&& !propertyNameOverriden)
{
return;
}
Expand Down
Expand Up @@ -11,13 +11,18 @@ public class NorthwindDbContext : DbContext

public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Customer> Customers { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>()
.HasComment("A category of products")
.Property(category => category.CategoryName)
.HasComment("The name of a category");
modelBuilder.Entity<Customer>(entity =>
{
entity.Property(e => e.CustomerKey).IsFixedLength();
});
}
}
}
100 changes: 100 additions & 0 deletions test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs
Expand Up @@ -16,6 +16,7 @@ namespace FakeNamespace
public partial class FakeDbContext : DbContext
{
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Product> Products { get; set; }
public FakeDbContext(DbContextOptions<FakeDbContext> options) : base(options)
Expand Down Expand Up @@ -45,6 +46,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasComment(""The name of a category"");
});
modelBuilder.Entity<Customer>(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<Product>(entity =>
{
entity.ToTable(""Product"");
Expand Down Expand Up @@ -87,6 +109,7 @@ namespace FakeNamespace
public partial class FakeDbContext : DbContext
{
public virtual DbSet<CategoryRenamed> Category { get; set; }
public virtual DbSet<CustomerRenamed> Customer { get; set; }
public virtual DbSet<ProductRenamed> Product { get; set; }
public FakeDbContext(DbContextOptions<FakeDbContext> options) : base(options)
Expand Down Expand Up @@ -121,6 +144,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasComment(""The name of a category"");
});
modelBuilder.Entity<CustomerRenamed>(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<ProductRenamed>(entity =>
{
entity.HasKey(e => e.ProductIdRenamed);
Expand Down Expand Up @@ -171,6 +215,7 @@ namespace FakeNamespace
public partial class FakeDbContext : DbContext
{
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Product> Products { get; set; }
public FakeDbContext(DbContextOptions<FakeDbContext> options) : base(options)
Expand All @@ -191,6 +236,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasComment(""The name of a category"");
});
modelBuilder.Entity<Customer>(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<Product>(entity =>
{
entity.ToTable(""Product"");
Expand Down Expand Up @@ -233,6 +299,7 @@ namespace FakeNamespace
public partial class FakeDbContext : DbContext
{
public virtual DbSet<CategoryRenamed> Category { get; set; }
public virtual DbSet<CustomerRenamed> Customer { get; set; }
public virtual DbSet<ProductRenamed> Product { get; set; }
public FakeDbContext(DbContextOptions<FakeDbContext> options) : base(options)
Expand All @@ -258,6 +325,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasComment(""The name of a category"");
});
modelBuilder.Entity<CustomerRenamed>(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<ProductRenamed>(entity =>
{
entity.HasKey(e => e.ProductIdRenamed);
Expand Down Expand Up @@ -308,6 +396,7 @@ namespace FakeNamespace
public partial class FakeDbContext : DbContext
{
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Product> Products { get; set; }
public FakeDbContext(DbContextOptions<FakeDbContext> options) : base(options)
Expand All @@ -332,6 +421,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.Property(e => e.CategoryName).HasComment(""The name of a category"");
});
modelBuilder.Entity<Customer>(entity =>
{
entity.Property(e => e.CustomerKey).IsFixedLength();
});
modelBuilder.Entity<Product>(entity =>
{
entity.Property(e => e.RowVersion)
Expand Down Expand Up @@ -360,6 +454,7 @@ namespace FakeNamespace
public partial class FakeDbContext : DbContext
{
public virtual DbSet<CategoryRenamed> Category { get; set; }
public virtual DbSet<CustomerRenamed> Customer { get; set; }
public virtual DbSet<ProductRenamed> Product { get; set; }
public FakeDbContext(DbContextOptions<FakeDbContext> options) : base(options)
Expand All @@ -384,6 +479,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.Property(e => e.CategoryNameRenamed).HasComment(""The name of a category"");
});
modelBuilder.Entity<CustomerRenamed>(entity =>
{
entity.Property(e => e.CustomerKey).IsFixedLength();
});
modelBuilder.Entity<ProductRenamed>(entity =>
{
entity.Property(e => e.RowVersion)
Expand Down
22 changes: 22 additions & 0 deletions test/Scaffolding.Handlebars.Tests/ExpectedTypeScriptContexts.cs
Expand Up @@ -16,6 +16,7 @@ namespace FakeNamespace
public partial class FakeDbContext : DbContext
{
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Product> Products { get; set; }
public FakeDbContext(DbContextOptions<FakeDbContext> options) : base(options)
Expand Down Expand Up @@ -45,6 +46,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasComment(""The name of a category"");
});
modelBuilder.Entity<Customer>(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<Product>(entity =>
{
entity.ToTable(""Product"");
Expand Down
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -611,7 +617,7 @@ private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions revEn
}

private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions revEngOptions,
Action<HandlebarsScaffoldingOptions> configureOptions,bool useEntityTransformMappings = false,
Action<HandlebarsScaffoldingOptions> configureOptions, bool useEntityTransformMappings = false,
string filenamePrefix = null, bool useAltTemplates = false)
{
HandlebarsLib.Configuration.NoEscape = true;
Expand Down Expand Up @@ -709,7 +715,8 @@ private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions revEn
|| 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;
Expand Down
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -437,7 +443,8 @@ private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions optio
|| 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;
Expand Down
2 changes: 2 additions & 0 deletions test/Scaffolding.Handlebars.Tests/Helpers/Constants.cs
Expand Up @@ -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";
}
}
Expand Down

0 comments on commit 2b65b77

Please sign in to comment.