Skip to content

Commit a946d4e

Browse files
committed
Generate HasKey if key property transformed.
1 parent 0be1fc9 commit a946d4e

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,17 +493,25 @@ private void GenerateKey(IKey key, IEntityType entityType, IndentedStringBuilder
493493
if (key.Properties.Count == 1
494494
&& annotations.Count == 0)
495495
{
496+
bool propertyNameOverriden = false;
497+
foreach (var property in key.Properties)
498+
{
499+
var transformedKeyName = EntityTypeTransformationService.TransformPropertyName(entityType, property.Name, property.DeclaringType.Name);
500+
propertyNameOverriden = !property.Name.Equals(transformedKeyName);
501+
if (propertyNameOverriden) break;
502+
}
503+
496504
if (key is IConventionKey concreteKey
497505
&& concreteKey.Properties.SequenceEqual(
498506
KeyDiscoveryConvention.DiscoverKeyProperties(
499507
concreteKey.DeclaringEntityType,
500-
concreteKey.DeclaringEntityType.GetProperties())))
508+
concreteKey.DeclaringEntityType.GetProperties()))
509+
&& UseDataAnnotations || !propertyNameOverriden)
501510
{
502511
return;
503512
}
504513

505-
if (!explicitName
506-
&& UseDataAnnotations)
514+
if (!explicitName && UseDataAnnotations)
507515
{
508516
return;
509517
}
@@ -534,14 +542,13 @@ private void GenerateTableName(IEntityType entityType, IndentedStringBuilder sb)
534542
var schema = entityType.GetSchema();
535543
var defaultSchema = entityType.Model.GetDefaultSchema();
536544

545+
var transformedTableName = EntityTypeTransformationService.TransformTypeEntityName(tableName);
546+
var tableNameOverriden = tableName != null && !tableName.Equals(transformedTableName);
547+
537548
var explicitSchema = schema != null && schema != defaultSchema;
538549
var explicitTable = explicitSchema || tableName != null && tableName != entityType.GetDbSetName();
539-
if (!explicitTable && tableName != null)
540-
{
541-
var overrideName = EntityTypeTransformationService.TransformTypeEntityName(tableName);
542-
if (!tableName.Equals(overrideName)) explicitTable = true;
543-
}
544-
if (explicitTable)
550+
if (!explicitTable && tableName != null && tableNameOverriden) explicitTable = true;
551+
if (explicitTable && tableName != null)
545552
{
546553
var parameterString = CSharpHelper.Literal(tableName);
547554
if (explicitSchema)
@@ -560,7 +567,7 @@ private void GenerateTableName(IEntityType entityType, IndentedStringBuilder sb)
560567
var explicitViewSchema = viewSchema != null && viewSchema != defaultSchema;
561568
var explicitViewTable = explicitViewSchema || viewName != null;
562569

563-
if (explicitViewTable)
570+
if (explicitViewTable && viewName != null)
564571
{
565572
var parameterString = CSharpHelper.Literal(viewName);
566573
if (explicitViewSchema)

test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
106106
{
107107
modelBuilder.Entity<CategoryRenamed>(entity =>
108108
{
109+
entity.HasKey(e => e.CategoryIdRenamed);
110+
109111
entity.ToTable(""Category"");
110112
111113
entity.HasComment(""A category of products"");
112114
115+
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
116+
113117
entity.Property(e => e.CategoryNameRenamed)
114118
.HasColumnName(""CategoryName"")
115119
.IsRequired()
@@ -119,9 +123,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
119123
120124
modelBuilder.Entity<ProductRenamed>(entity =>
121125
{
126+
entity.HasKey(e => e.ProductIdRenamed);
127+
122128
entity.ToTable(""Product"");
123129
124-
entity.HasIndex(e => e.CategoryId, ""IX_Product_CategoryId"");
130+
entity.HasIndex(e => e.CategoryIdRenamed, ""IX_Product_CategoryId"");
131+
132+
entity.Property(e => e.ProductIdRenamed).HasColumnName(""ProductId"");
133+
134+
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
125135
126136
entity.Property(e => e.ProductName)
127137
.IsRequired()
@@ -137,7 +147,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
137147
138148
entity.HasOne(d => d.Category)
139149
.WithMany(p => p.Product)
140-
.HasForeignKey(d => d.CategoryId);
150+
.HasForeignKey(d => d.CategoryIdRenamed);
141151
});
142152
143153
OnModelCreatingPartial(modelBuilder);
@@ -233,10 +243,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
233243
{
234244
modelBuilder.Entity<CategoryRenamed>(entity =>
235245
{
246+
entity.HasKey(e => e.CategoryIdRenamed);
247+
236248
entity.ToTable(""Category"");
237249
238250
entity.HasComment(""A category of products"");
239251
252+
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
253+
240254
entity.Property(e => e.CategoryNameRenamed)
241255
.HasColumnName(""CategoryName"")
242256
.IsRequired()
@@ -246,9 +260,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
246260
247261
modelBuilder.Entity<ProductRenamed>(entity =>
248262
{
263+
entity.HasKey(e => e.ProductIdRenamed);
264+
249265
entity.ToTable(""Product"");
250266
251-
entity.HasIndex(e => e.CategoryId, ""IX_Product_CategoryId"");
267+
entity.HasIndex(e => e.CategoryIdRenamed, ""IX_Product_CategoryId"");
268+
269+
entity.Property(e => e.ProductIdRenamed).HasColumnName(""ProductId"");
270+
271+
entity.Property(e => e.CategoryIdRenamed).HasColumnName(""CategoryId"");
252272
253273
entity.Property(e => e.ProductName)
254274
.IsRequired()
@@ -264,7 +284,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
264284
265285
entity.HasOne(d => d.Category)
266286
.WithMany(p => p.Product)
267-
.HasForeignKey(d => d.CategoryId);
287+
.HasForeignKey(d => d.CategoryIdRenamed);
268288
});
269289
270290
OnModelCreatingPartial(modelBuilder);

test/Scaffolding.Handlebars.Tests/ExpectedEntities.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public CategoryRenamed()
7171
Products = new HashSet<ProductRenamed>();
7272
}
7373
74-
public int CategoryId { get; set; }
74+
public int CategoryIdRenamed { get; set; }
7575
7676
/// <summary>
7777
/// The name of a category
@@ -91,12 +91,12 @@ namespace FakeNamespace
9191
{
9292
public partial class ProductRenamed
9393
{
94-
public int ProductId { get; set; }
94+
public int ProductIdRenamed { get; set; }
9595
public string ProductName { get; set; }
9696
public decimal? UnitPriceRenamed { get; set; }
9797
public bool Discontinued { get; set; }
9898
public byte[] RowVersion { get; set; }
99-
public int? CategoryId { get; set; }
99+
public int? CategoryIdRenamed { get; set; }
100100
101101
public virtual CategoryRenamed Category { get; set; }
102102
}
@@ -197,7 +197,8 @@ public CategoryRenamed()
197197
}
198198
199199
[Key]
200-
public int CategoryId { get; set; }
200+
[Column(""CategoryId"")]
201+
public int CategoryIdRenamed { get; set; }
201202
202203
/// <summary>
203204
/// The name of a category
@@ -227,17 +228,19 @@ namespace FakeNamespace
227228
public partial class ProductRenamed
228229
{
229230
[Key]
230-
public int ProductId { get; set; }
231+
[Column(""ProductId"")]
232+
public int ProductIdRenamed { get; set; }
231233
[Required]
232234
[StringLength(40)]
233235
public string ProductName { get; set; }
234236
[Column(""UnitPrice"", TypeName = ""money"")]
235237
public decimal? UnitPriceRenamed { get; set; }
236238
public bool Discontinued { get; set; }
237239
public byte[] RowVersion { get; set; }
238-
public int? CategoryId { get; set; }
240+
[Column(""CategoryId"")]
241+
public int? CategoryIdRenamed { get; set; }
239242
240-
[ForeignKey(nameof(CategoryId))]
243+
[ForeignKey(nameof(CategoryIdRenamed))]
241244
[InverseProperty(""Products"")]
242245
public virtual CategoryRenamed Category { get; set; }
243246
}

test/Scaffolding.Handlebars.Tests/Helpers/HandlebarsTransformers.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ namespace Scaffolding.Handlebars.Tests.Helpers
55
{
66
public static class HandlebarsTransformers
77
{
8-
static readonly Dictionary<string, string> _entityTypeNameMappings = new(){
9-
{"Product","ProductRenamed"},
10-
{"Category","CategoryRenamed"}
8+
static readonly Dictionary<string, string> _entityTypeNameMappings = new()
9+
{
10+
{ "Product","ProductRenamed" },
11+
{ "Category","CategoryRenamed" }
1112
};
12-
static readonly Dictionary<string, string> _entityPropertyNameMappings = new(){
13-
{"UnitPrice","UnitPriceRenamed"},
14-
{"CategoryName","CategoryNameRenamed"}
13+
static readonly Dictionary<string, string> _entityPropertyNameMappings = new()
14+
{
15+
{ "ProductId", "ProductIdRenamed" },
16+
{ "UnitPrice","UnitPriceRenamed"},
17+
{ "CategoryId", "CategoryIdRenamed" },
18+
{ "CategoryName","CategoryNameRenamed" }
1519
};
1620
public static string MapEntityName(string entityName) =>
1721
_entityTypeNameMappings.TryGetValue(entityName, out var nameOverride) ? nameOverride : entityName;

0 commit comments

Comments
 (0)