Skip to content

Commit

Permalink
Don't generate Fluent API in model snapshot for certain annotations
Browse files Browse the repository at this point in the history
Temporary fix for 5.0.

Fixes #23456
  • Loading branch information
roji committed Jan 11, 2021
1 parent 4149be5 commit 25ab6f9
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 123 deletions.
52 changes: 49 additions & 3 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Design;
Expand Down Expand Up @@ -64,6 +65,16 @@ public virtual void Generate(string builderName, IModel model, IndentedStringBui

using (stringBuilder.Indent())
{
// Temporary patch: specifically exclude some annotations which are known to produce identical Fluent API calls across different
// providers, generating them as raw annotations instead.
var ambiguousAnnotations = RemoveAmbiguousFluentApiAnnotations(
annotations,
name => name.EndsWith(":ValueGenerationStrategy", StringComparison.Ordinal)
|| name.EndsWith(":IdentityIncrement", StringComparison.Ordinal)
|| name.EndsWith(":IdentitySeed", StringComparison.Ordinal)
|| name.EndsWith(":HiLoSequenceName", StringComparison.Ordinal)
|| name.EndsWith(":HiLoSequenceSchema", StringComparison.Ordinal));

foreach (var methodCallCodeFragment in
Dependencies.AnnotationCodeGenerator.GenerateFluentApiCalls(model, annotations))
{
Expand All @@ -79,7 +90,7 @@ public virtual void Generate(string builderName, IModel model, IndentedStringBui
new Annotation(CoreAnnotationNames.ProductVersion, productVersion));
}

GenerateAnnotations(remainingAnnotations, stringBuilder);
GenerateAnnotations(remainingAnnotations.Concat(ambiguousAnnotations), stringBuilder);
}

stringBuilder.AppendLine(";");
Expand Down Expand Up @@ -574,6 +585,16 @@ protected virtual void GeneratePropertyAnnotations([NotNull] IProperty property,
GenerateFluentApiForDefaultValue(property, stringBuilder);
annotations.Remove(RelationalAnnotationNames.DefaultValue);

// Temporary patch: specifically exclude some annotations which are known to produce identical Fluent API calls across different
// providers, generating them as raw annotations instead.
var ambiguousAnnotations = RemoveAmbiguousFluentApiAnnotations(
annotations,
name => name.EndsWith(":ValueGenerationStrategy", StringComparison.Ordinal)
|| name.EndsWith(":IdentityIncrement", StringComparison.Ordinal)
|| name.EndsWith(":IdentitySeed", StringComparison.Ordinal)
|| name.EndsWith(":HiLoSequenceName", StringComparison.Ordinal)
|| name.EndsWith(":HiLoSequenceSchema", StringComparison.Ordinal));

foreach (var methodCallCodeFragment in
Dependencies.AnnotationCodeGenerator.GenerateFluentApiCalls(property, annotations))
{
Expand All @@ -582,7 +603,7 @@ protected virtual void GeneratePropertyAnnotations([NotNull] IProperty property,
.Append(Code.Fragment(methodCallCodeFragment));
}

GenerateAnnotations(annotations.Values, stringBuilder);
GenerateAnnotations(annotations.Values.Concat(ambiguousAnnotations), stringBuilder);
}

private ValueConverter FindValueConverter(IProperty property)
Expand Down Expand Up @@ -765,6 +786,12 @@ protected virtual void GenerateIndexAnnotations(
.FilterIgnoredAnnotations(index.GetAnnotations())
.ToDictionary(a => a.Name, a => a);

// Temporary patch: specifically exclude some annotations which are known to produce identical Fluent API calls across different
// providers, generating them as raw annotations instead.
var ambiguousAnnotations = RemoveAmbiguousFluentApiAnnotations(
annotations,
name => name.EndsWith(":Include", StringComparison.Ordinal));

foreach (var methodCallCodeFragment in
Dependencies.AnnotationCodeGenerator.GenerateFluentApiCalls(index, annotations))
{
Expand All @@ -773,7 +800,7 @@ protected virtual void GenerateIndexAnnotations(
.Append(Code.Fragment(methodCallCodeFragment));
}

GenerateAnnotations(annotations.Values, stringBuilder);
GenerateAnnotations(annotations.Values.Concat(ambiguousAnnotations), stringBuilder);
}

/// <summary>
Expand Down Expand Up @@ -1565,5 +1592,24 @@ private void GenerateFluentApiForDefaultValue(
stringBuilder
.Append(")");
}

private static IReadOnlyList<IAnnotation> RemoveAmbiguousFluentApiAnnotations(
Dictionary<string, IAnnotation> annotations,
Func<string, bool> annotationNameMatcher)
{
List<IAnnotation> ambiguousAnnotations = null;

foreach (var (name, annotation) in annotations)
{
if (annotationNameMatcher(name))
{
annotations.Remove(name);
ambiguousAnnotations ??= new List<IAnnotation>();
ambiguousAnnotations.Add(annotation);
}
}

return (IReadOnlyList<IAnnotation>)ambiguousAnnotations ?? ImmutableList<IAnnotation>.Empty;
}
}
}
Loading

0 comments on commit 25ab6f9

Please sign in to comment.