diff --git a/README.md b/README.md index 2a775fb..04fd37e 100644 --- a/README.md +++ b/README.md @@ -256,20 +256,20 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices // Add Handlebars transformer for Country property services.AddHandlebarsTransformers( - propertyTransformer: (e, p) => + propertyTransformer: p => p.PropertyName == "Country" ? new EntityPropertyInfo("Country?", p.PropertyName, false) : new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable)); // Add Handlebars transformer for Id property - //services.AddHandlebarsTransformers( + //services.AddHandlebarsTransformers2( // propertyTransformer: (e, p) => // $"{e.Name}Id" == p.PropertyName // ? new EntityPropertyInfo(p.PropertyType, "Id", false) // : new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable)); // Add optional Handlebars transformers - //services.AddHandlebarsTransformers( + //services.AddHandlebarsTransformers2( // entityTypeNameTransformer: n => n + "Foo", // entityFileNameTransformer: n => n + "Foo", // constructorTransformer: (e, p) => new EntityPropertyInfo(p.PropertyType + "Foo", p.PropertyName + "Foo"), diff --git a/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs b/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs index dc361f5..f40860f 100644 --- a/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs +++ b/sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs @@ -56,20 +56,20 @@ public void ConfigureDesignTimeServices(IServiceCollection services) // Add Handlebars transformer for Country property services.AddHandlebarsTransformers( - propertyTransformer: (e, p) => + propertyTransformer: p => p.PropertyName == "Country" ? new EntityPropertyInfo("Country?", p.PropertyName, false) : new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable)); // Add Handlebars transformer for Id property - //services.AddHandlebarsTransformers( + //services.AddHandlebarsTransformers2( // propertyTransformer: (e, p) => // $"{e.Name}Id" == p.PropertyName // ? new EntityPropertyInfo(p.PropertyType, "Id", false) // : new EntityPropertyInfo(p.PropertyType, p.PropertyName, p.PropertyIsNullable)); // Add optional Handlebars transformers - //services.AddHandlebarsTransformers( + //services.AddHandlebarsTransformers2( // entityTypeNameTransformer: n => n + "Foo", // entityFileNameTransformer: n => n + "Foo", // constructorTransformer: (e, p) => new EntityPropertyInfo(p.PropertyType + "Foo", p.PropertyName + "Foo"), diff --git a/src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj b/src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj index 6e7493e..61aaf75 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.0 + 6.0.1 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.0 + See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.1 latest true true diff --git a/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpEntityTypeGenerator.cs b/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpEntityTypeGenerator.cs index f640128..5acbbd1 100644 --- a/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpEntityTypeGenerator.cs +++ b/src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpEntityTypeGenerator.cs @@ -713,21 +713,34 @@ private void GenerateForeignKeyAttribute(IEntityType entityType, ISkipNavigation { var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); + bool propertyNameOverriden = false; if (navigation.ForeignKey.Properties.Count > 1) { + foreach (var property in navigation.ForeignKey.Properties) + { + var transformedKeyName = EntityTypeTransformationService.TransformPropertyName(entityType, property.Name, property.DeclaringType.Name); + propertyNameOverriden = !property.Name.Equals(transformedKeyName); + if (propertyNameOverriden) break; + } + foreignKeyAttribute.AddParameter( CSharpHelper.Literal( string.Join(",", navigation.ForeignKey.Properties.Select(p => EntityTypeTransformationService.TransformNavPropertyName(entityType, p.Name, p.ClrType.Name))))); } else { - foreignKeyAttribute.AddParameter($"nameof({EntityTypeTransformationService.TransformNavPropertyName(entityType, navigation.ForeignKey.Properties.First().Name, navigation.ForeignKey.Properties.First().ClrType.Name)})"); + var transformedKeyName = EntityTypeTransformationService.TransformPropertyName(entityType, navigation.ForeignKey.Properties.First().Name, navigation.ForeignKey.Properties.First().DeclaringType.Name); + propertyNameOverriden = !navigation.ForeignKey.Properties.First().Name.Equals(transformedKeyName); + foreignKeyAttribute.AddParameter($"nameof({transformedKeyName})"); } - NavPropertyAnnotations.Add(new Dictionary + if (!propertyNameOverriden) { - { "nav-property-annotation", foreignKeyAttribute } - }); + NavPropertyAnnotations.Add(new Dictionary + { + { "nav-property-annotation", foreignKeyAttribute } + }); + } } } diff --git a/src/EntityFrameworkCore.Scaffolding.Handlebars/ServiceCollectionExtensions.cs b/src/EntityFrameworkCore.Scaffolding.Handlebars/ServiceCollectionExtensions.cs index a96556a..77cca6f 100644 --- a/src/EntityFrameworkCore.Scaffolding.Handlebars/ServiceCollectionExtensions.cs +++ b/src/EntityFrameworkCore.Scaffolding.Handlebars/ServiceCollectionExtensions.cs @@ -230,7 +230,7 @@ public static IServiceCollection AddHandlebarsTransformers(this IServiceCollecti /// Navigation property name transformer. /// Context file name transformer. /// The same service collection so that multiple calls can be chained. - public static IServiceCollection AddHandlebarsTransformers(this IServiceCollection services, + public static IServiceCollection AddHandlebarsTransformers2(this IServiceCollection services, Func entityTypeNameTransformer = null, Func entityFileNameTransformer = null, Func constructorTransformer = null,