Skip to content

Commit

Permalink
Generate HasKey if key property transformed.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysneed committed Dec 18, 2021
1 parent 0be1fc9 commit a946d4e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,17 +493,25 @@ private void GenerateKey(IKey key, IEntityType entityType, IndentedStringBuilder
if (key.Properties.Count == 1
&& annotations.Count == 0)
{
bool propertyNameOverriden = false;
foreach (var property in key.Properties)
{
var transformedKeyName = EntityTypeTransformationService.TransformPropertyName(entityType, property.Name, property.DeclaringType.Name);
propertyNameOverriden = !property.Name.Equals(transformedKeyName);
if (propertyNameOverriden) break;
}

if (key is IConventionKey concreteKey
&& concreteKey.Properties.SequenceEqual(
KeyDiscoveryConvention.DiscoverKeyProperties(
concreteKey.DeclaringEntityType,
concreteKey.DeclaringEntityType.GetProperties())))
concreteKey.DeclaringEntityType.GetProperties()))
&& UseDataAnnotations || !propertyNameOverriden)
{
return;
}

if (!explicitName
&& UseDataAnnotations)
if (!explicitName && UseDataAnnotations)
{
return;
}
Expand Down Expand Up @@ -534,14 +542,13 @@ private void GenerateTableName(IEntityType entityType, IndentedStringBuilder sb)
var schema = entityType.GetSchema();
var defaultSchema = entityType.Model.GetDefaultSchema();

var transformedTableName = EntityTypeTransformationService.TransformTypeEntityName(tableName);
var tableNameOverriden = tableName != null && !tableName.Equals(transformedTableName);

var explicitSchema = schema != null && schema != defaultSchema;
var explicitTable = explicitSchema || tableName != null && tableName != entityType.GetDbSetName();
if (!explicitTable && tableName != null)
{
var overrideName = EntityTypeTransformationService.TransformTypeEntityName(tableName);
if (!tableName.Equals(overrideName)) explicitTable = true;
}
if (explicitTable)
if (!explicitTable && tableName != null && tableNameOverriden) explicitTable = true;
if (explicitTable && tableName != null)
{
var parameterString = CSharpHelper.Literal(tableName);
if (explicitSchema)
Expand All @@ -560,7 +567,7 @@ private void GenerateTableName(IEntityType entityType, IndentedStringBuilder sb)
var explicitViewSchema = viewSchema != null && viewSchema != defaultSchema;
var explicitViewTable = explicitViewSchema || viewName != null;

if (explicitViewTable)
if (explicitViewTable && viewName != null)
{
var parameterString = CSharpHelper.Literal(viewName);
if (explicitViewSchema)
Expand Down
28 changes: 24 additions & 4 deletions test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CategoryRenamed>(entity =>
{
entity.HasKey(e => e.CategoryIdRenamed);
entity.ToTable(""Category"");
entity.HasComment(""A category of products"");
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
entity.Property(e => e.CategoryNameRenamed)
.HasColumnName(""CategoryName"")
.IsRequired()
Expand All @@ -119,9 +123,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<ProductRenamed>(entity =>
{
entity.HasKey(e => e.ProductIdRenamed);
entity.ToTable(""Product"");
entity.HasIndex(e => e.CategoryId, ""IX_Product_CategoryId"");
entity.HasIndex(e => e.CategoryIdRenamed, ""IX_Product_CategoryId"");
entity.Property(e => e.ProductIdRenamed).HasColumnName(""ProductId"");
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
entity.Property(e => e.ProductName)
.IsRequired()
Expand All @@ -137,7 +147,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.HasOne(d => d.Category)
.WithMany(p => p.Product)
.HasForeignKey(d => d.CategoryId);
.HasForeignKey(d => d.CategoryIdRenamed);
});
OnModelCreatingPartial(modelBuilder);
Expand Down Expand Up @@ -233,10 +243,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CategoryRenamed>(entity =>
{
entity.HasKey(e => e.CategoryIdRenamed);
entity.ToTable(""Category"");
entity.HasComment(""A category of products"");
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
entity.Property(e => e.CategoryNameRenamed)
.HasColumnName(""CategoryName"")
.IsRequired()
Expand All @@ -246,9 +260,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<ProductRenamed>(entity =>
{
entity.HasKey(e => e.ProductIdRenamed);
entity.ToTable(""Product"");
entity.HasIndex(e => e.CategoryId, ""IX_Product_CategoryId"");
entity.HasIndex(e => e.CategoryIdRenamed, ""IX_Product_CategoryId"");
entity.Property(e => e.ProductIdRenamed).HasColumnName(""ProductId"");
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
entity.Property(e => e.ProductName)
.IsRequired()
Expand All @@ -264,7 +284,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.HasOne(d => d.Category)
.WithMany(p => p.Product)
.HasForeignKey(d => d.CategoryId);
.HasForeignKey(d => d.CategoryIdRenamed);
});
OnModelCreatingPartial(modelBuilder);
Expand Down
17 changes: 10 additions & 7 deletions test/Scaffolding.Handlebars.Tests/ExpectedEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public CategoryRenamed()
Products = new HashSet<ProductRenamed>();
}
public int CategoryId { get; set; }
public int CategoryIdRenamed { get; set; }
/// <summary>
/// The name of a category
Expand All @@ -91,12 +91,12 @@ namespace FakeNamespace
{
public partial class ProductRenamed
{
public int ProductId { get; set; }
public int ProductIdRenamed { get; set; }
public string ProductName { get; set; }
public decimal? UnitPriceRenamed { get; set; }
public bool Discontinued { get; set; }
public byte[] RowVersion { get; set; }
public int? CategoryId { get; set; }
public int? CategoryIdRenamed { get; set; }
public virtual CategoryRenamed Category { get; set; }
}
Expand Down Expand Up @@ -197,7 +197,8 @@ public CategoryRenamed()
}
[Key]
public int CategoryId { get; set; }
[Column(""CategoryId"")]
public int CategoryIdRenamed { get; set; }
/// <summary>
/// The name of a category
Expand Down Expand Up @@ -227,17 +228,19 @@ namespace FakeNamespace
public partial class ProductRenamed
{
[Key]
public int ProductId { get; set; }
[Column(""ProductId"")]
public int ProductIdRenamed { get; set; }
[Required]
[StringLength(40)]
public string ProductName { get; set; }
[Column(""UnitPrice"", TypeName = ""money"")]
public decimal? UnitPriceRenamed { get; set; }
public bool Discontinued { get; set; }
public byte[] RowVersion { get; set; }
public int? CategoryId { get; set; }
[Column(""CategoryId"")]
public int? CategoryIdRenamed { get; set; }
[ForeignKey(nameof(CategoryId))]
[ForeignKey(nameof(CategoryIdRenamed))]
[InverseProperty(""Products"")]
public virtual CategoryRenamed Category { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ namespace Scaffolding.Handlebars.Tests.Helpers
{
public static class HandlebarsTransformers
{
static readonly Dictionary<string, string> _entityTypeNameMappings = new(){
{"Product","ProductRenamed"},
{"Category","CategoryRenamed"}
static readonly Dictionary<string, string> _entityTypeNameMappings = new()
{
{ "Product","ProductRenamed" },
{ "Category","CategoryRenamed" }
};
static readonly Dictionary<string, string> _entityPropertyNameMappings = new(){
{"UnitPrice","UnitPriceRenamed"},
{"CategoryName","CategoryNameRenamed"}
static readonly Dictionary<string, string> _entityPropertyNameMappings = new()
{
{ "ProductId", "ProductIdRenamed" },
{ "UnitPrice","UnitPriceRenamed"},
{ "CategoryId", "CategoryIdRenamed" },
{ "CategoryName","CategoryNameRenamed" }
};
public static string MapEntityName(string entityName) =>
_entityTypeNameMappings.TryGetValue(entityName, out var nameOverride) ? nameOverride : entityName;
Expand Down

0 comments on commit a946d4e

Please sign in to comment.