Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class HbsCSharpDbContextGenerator : CSharpDbContextGenerator
/// <param name="entityTypeTransformationService">Service for transforming entity definitions.</param>
/// <param name="options">Handlebars scaffolding options.</param>
public HbsCSharpDbContextGenerator(
[NotNull] IProviderConfigurationCodeGenerator providerConfigurationCodeGenerator,
[NotNull] IProviderConfigurationCodeGenerator providerConfigurationCodeGenerator,
[NotNull] IAnnotationCodeGenerator annotationCodeGenerator,
[NotNull] IDbContextTemplateService dbContextTemplateService,
[NotNull] IEntityTypeTransformationService entityTypeTransformationService,
Expand Down Expand Up @@ -137,7 +137,7 @@ public override string WriteCode(IModel model, string contextName, string connec
/// <param name="useDataAnnotations">Use fluent modeling API if false.</param>
/// <param name="suppressConnectionStringWarning">Suppress connection string warning.</param>
protected override void GenerateClass(
IModel model, string contextName, string connectionString, bool useDataAnnotations,
IModel model, string contextName, string connectionString, bool useDataAnnotations,
bool suppressConnectionStringWarning)
{
Check.NotNull(model, nameof(model));
Expand Down Expand Up @@ -206,7 +206,7 @@ protected override void GenerateOnConfiguring(string connectionString, bool supp
sb.AppendLine("}");
}

sb.AppendLine("}");
sb.AppendLine("}");
}

var onConfiguring = sb.ToString();
Expand Down Expand Up @@ -501,7 +501,7 @@ private void GenerateKey(IKey key, IEntityType entityType, bool useDataAnnotatio

var lines = new List<string>
{
$".{nameof(EntityTypeBuilder.HasKey)}(e => {GenerateLambdaToKey(key.Properties, "e")})"
$".{nameof(EntityTypeBuilder.HasKey)}(e => {GenerateLambdaToKey(key.Properties, "e", EntityTypeTransformationService.TransformPropertyName)})"
};

if (explicitName)
Expand Down Expand Up @@ -567,7 +567,7 @@ private void GenerateIndex(IIndex index, IndentedStringBuilder sb)
{
var lines = new List<string>
{
$".{nameof(EntityTypeBuilder.HasIndex)}(e => {GenerateLambdaToKey(index.Properties, "e")})"
$".{nameof(EntityTypeBuilder.HasIndex)}(e => {GenerateLambdaToKey(index.Properties, "e", EntityTypeTransformationService.TransformPropertyName)})"
};

var annotations = index.GetAnnotations().ToList();
Expand Down Expand Up @@ -622,7 +622,7 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations, Inden
{
var lines = new List<string>
{
$".{nameof(EntityTypeBuilder.Property)}(e => e.{property.Name})"
$".{nameof(EntityTypeBuilder.Property)}(e => e.{EntityTypeTransformationService.TransformPropertyName(property.Name)})"
};

var annotations = property.GetAnnotations().ToList();
Expand Down Expand Up @@ -799,9 +799,9 @@ private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotation

var lines = new List<string>
{
$".{nameof(EntityTypeBuilder.HasOne)}(" + (foreignKey.DependentToPrincipal != null ? $"d => d.{foreignKey.DependentToPrincipal.Name}" : null) + ")",
$".{nameof(EntityTypeBuilder.HasOne)}(" + (foreignKey.DependentToPrincipal != null ? $"d => d.{EntityTypeTransformationService.TransformNavPropertyName(foreignKey.DependentToPrincipal.Name)}" : null) + ")",
$".{(foreignKey.IsUnique ? nameof(ReferenceNavigationBuilder.WithOne) : nameof(ReferenceNavigationBuilder.WithMany))}"
+ $"(" + (foreignKey.PrincipalToDependent != null ? $"p => p.{foreignKey.PrincipalToDependent.Name}" : null) + ")"
+ $"(" + (foreignKey.PrincipalToDependent != null ? $"p => p.{EntityTypeTransformationService.TransformNavPropertyName(foreignKey.PrincipalToDependent.Name)}" : null) + ")"
};

if (!foreignKey.PrincipalKey.IsPrimaryKey())
Expand All @@ -810,13 +810,13 @@ private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotation
lines.Add(
$".{nameof(ReferenceReferenceBuilder.HasPrincipalKey)}"
+ (foreignKey.IsUnique ? $"<{((ITypeBase)foreignKey.PrincipalEntityType).DisplayName()}>" : "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 812 should be:

+ (foreignKey.IsUnique ? $"<{EntityTypeTransformationService.TransformPropertyName(((ITypeBase)foreignKey.PrincipalEntityType).DisplayName())}>" : "")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmayhak You still need to make this change, so that generic type arguments are transformed.

+ $"(p => {GenerateLambdaToKey(foreignKey.PrincipalKey.Properties, "p")})");
+ $"(p => {GenerateLambdaToKey(foreignKey.PrincipalKey.Properties, "p", EntityTypeTransformationService.TransformNavPropertyName)})");
}

lines.Add(
$".{nameof(ReferenceReferenceBuilder.HasForeignKey)}"
+ (foreignKey.IsUnique ? $"<{((ITypeBase)foreignKey.DeclaringEntityType).DisplayName()}>" : "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 818 should be:

+ (foreignKey.IsUnique ? $"<{EntityTypeTransformationService.TransformPropertyName(((ITypeBase)foreignKey.DeclaringEntityType).DisplayName())}>" : "")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmayhak You still need to make this change, so that generic type arguments are transformed.

+ $"(d => {GenerateLambdaToKey(foreignKey.Properties, "d")})");
+ $"(d => {GenerateLambdaToKey(foreignKey.Properties, "d", EntityTypeTransformationService.TransformPropertyName)})");

var defaultOnDeleteAction = foreignKey.IsRequired
? DeleteBehavior.Cascade
Expand Down Expand Up @@ -939,15 +939,16 @@ private void GenerateSequence(ISequence sequence, IndentedStringBuilder sb)
sb.AppendLine(";");
}

private static string GenerateLambdaToKey(
private string GenerateLambdaToKey(
IReadOnlyList<IProperty> properties,
string lambdaIdentifier)
string lambdaIdentifier,
Func<string, string> nameTransform )
{
return properties.Count <= 0
? ""
: properties.Count == 1
? $"{lambdaIdentifier}.{properties[0].Name}"
: $"new {{ {string.Join(", ", properties.Select(p => lambdaIdentifier + "." + p.Name))} }}";
? $"{lambdaIdentifier}.{nameTransform(properties[0].Name)}"
: $"new {{ {string.Join(", ", properties.Select(p => lambdaIdentifier + "." + nameTransform(p.Name)))} }}";
}

private static void RemoveAnnotation(ref List<IAnnotation> annotations, string annotationName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public string TransformPropertyName(string propertyName)
return PropertyTransformer?.Invoke(propTypeInfo)?.PropertyName ?? propertyName;
}

/// <summary>
/// Transform single navigation property name.
/// </summary>
/// <param name="propertyName">Property name.</param>
/// <returns>Transformed property name.</returns>
public string TransformNavPropertyName(string propertyName)
{
var propTypeInfo = new EntityPropertyInfo { PropertyName = propertyName };
return NavPropertyTransformer?.Invoke(propTypeInfo)?.PropertyName ?? propertyName;
}

/// <summary>
/// Transform entity type constructor.
/// </summary>
Expand Down Expand Up @@ -118,7 +129,7 @@ public List<Dictionary<string, object>> TransformProperties(List<Dictionary<stri

foreach (var property in properties)
{
var propTypeInfo = new EntityPropertyInfo(property["property-type"] as string,
var propTypeInfo = new EntityPropertyInfo(property["property-type"] as string,
property["property-name"] as string);
var transformedProp = PropertyTransformer?.Invoke(propTypeInfo) ?? propTypeInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public interface IEntityTypeTransformationService
/// <returns>Transformed property name.</returns>
string TransformPropertyName(string propertyName);

/// <summary>
/// Transform single navigation property name.
/// </summary>
/// <param name="propertyName">Property name.</param>
/// <returns>Transformed property name.</returns>
string TransformNavPropertyName(string propertyName);

/// <summary>
/// Transform entity type constructor.
/// </summary>
Expand Down