Skip to content

Commit

Permalink
Merge pull request #202 from TrackableEntities/fix-ambiguous-method
Browse files Browse the repository at this point in the history
Fix ambiguous method AddHandlebarsTransformers
  • Loading branch information
tonysneed authored Dec 21, 2021
2 parents 2a50b3a + 2494b24 commit 5d85c1e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
6 changes: 3 additions & 3 deletions sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>6.0.0</Version>
<Version>6.0.1</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.0</PackageReleaseNotes>
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.1</PackageReleaseNotes>
<LangVersion>latest</LangVersion>
<IncludeSource>true</IncludeSource>
<SignAssembly>true</SignAssembly>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>
if (!propertyNameOverriden)
{
{ "nav-property-annotation", foreignKeyAttribute }
});
NavPropertyAnnotations.Add(new Dictionary<string, object>
{
{ "nav-property-annotation", foreignKeyAttribute }
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public static IServiceCollection AddHandlebarsTransformers(this IServiceCollecti
/// <param name="navPropertyTransformer">Navigation property name transformer.</param>
/// <param name="contextFileNameTransformer">Context file name transformer.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddHandlebarsTransformers(this IServiceCollection services,
public static IServiceCollection AddHandlebarsTransformers2(this IServiceCollection services,
Func<string, string> entityTypeNameTransformer = null,
Func<string, string> entityFileNameTransformer = null,
Func<IEntityType, EntityPropertyInfo, EntityPropertyInfo> constructorTransformer = null,
Expand Down

0 comments on commit 5d85c1e

Please sign in to comment.