Skip to content
Merged
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
15 changes: 10 additions & 5 deletions src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
{
var fragments = new List<MethodCallCodeFragment>(base.GenerateFluentApiCalls(model, annotations));

if (GenerateValueGenerationStrategy(annotations, onModel: true) is { } valueGenerationStrategy)
if (GenerateValueGenerationStrategy(annotations, model, onModel: true) is { } valueGenerationStrategy)
{
fragments.Add(valueGenerationStrategy);
}
Expand Down Expand Up @@ -307,7 +307,7 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
{
var fragments = new List<MethodCallCodeFragment>(base.GenerateFluentApiCalls(property, annotations));

if (GenerateValueGenerationStrategy(annotations, onModel: false) is { } valueGenerationStrategy)
if (GenerateValueGenerationStrategy(annotations, property.DeclaringType.Model, onModel: false) is { } valueGenerationStrategy)
{
fragments.Add(valueGenerationStrategy);
}
Expand All @@ -320,7 +320,10 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
return fragments;
}

private MethodCallCodeFragment? GenerateValueGenerationStrategy(IDictionary<string, IAnnotation> annotations, bool onModel)
private MethodCallCodeFragment? GenerateValueGenerationStrategy(
IDictionary<string, IAnnotation> annotations,
IModel model,
bool onModel)
{
if (!TryGetAndRemove(annotations, NpgsqlAnnotationNames.ValueGenerationStrategy, out NpgsqlValueGenerationStrategy strategy))
{
Expand All @@ -342,8 +345,10 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(

case NpgsqlValueGenerationStrategy.SequenceHiLo:
{
var name = GetAndRemove<string>(NpgsqlAnnotationNames.HiLoSequenceName)!;
var schema = GetAndRemove<string>(NpgsqlAnnotationNames.HiLoSequenceSchema);
var name = GetAndRemove<string>(NpgsqlAnnotationNames.HiLoSequenceName)
?? (onModel ? null : model.GetHiLoSequenceName());
var schema = GetAndRemove<string>(NpgsqlAnnotationNames.HiLoSequenceSchema)
?? (onModel ? null : model.GetHiLoSequenceSchema());
return new MethodCallCodeFragment(
onModel ? ModelUseHiLoMethodInfo : PropertyUseHiLoMethodInfo,
(name, schema) switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public void GenerateFluentApi_IProperty_works_with_HiLo()
{
var generator = CreateGenerator();
var modelBuilder = new ModelBuilder(NpgsqlConventionSetBuilder.Build());
modelBuilder.UseHiLo("ModelHiLoIndexName", "ModelHiLoIndexSchema");
modelBuilder.Entity("Post", x => x.Property<int>("Id").UseHiLo("HiLoIndexName", "HiLoIndexSchema"));
var property = (IProperty)modelBuilder.Model.FindEntityType("Post").FindProperty("Id");

Expand All @@ -212,6 +213,31 @@ public void GenerateFluentApi_IProperty_works_with_HiLo()
schema => Assert.Equal("HiLoIndexSchema", schema));
}

[ConditionalTheory]
[InlineData(null, null)]
[InlineData("HiLoIndexName", null)]
[InlineData(null, "HiLoIndexSchema")]
[InlineData("HiLoIndexName", "HiLoIndexSchema")]
public void GenerateFluentApi_IProperty_works_with_inherited_HiLo(string name, string schema)
{
var generator = CreateGenerator();
var modelBuilder = new ModelBuilder(NpgsqlConventionSetBuilder.Build());
modelBuilder.UseHiLo(name, schema);
modelBuilder.Entity("Post", x => x.Property<int>("Id"));
var property = modelBuilder.FinalizeModel().FindEntityType("Post").FindProperty("Id");

var annotations = property.GetAnnotations().ToDictionary(a => a.Name, a => a);
var result = generator.GenerateFluentApiCalls(property, annotations).Single();

Assert.Equal("UseHiLo", result.Method);
Assert.Equal("NpgsqlPropertyBuilderExtensions", result.DeclaringType);

object[] expectedArguments = schema is null
? [name ?? NpgsqlModelExtensions.DefaultHiLoSequenceName]
: [name ?? NpgsqlModelExtensions.DefaultHiLoSequenceName, schema];
Assert.Equal(expectedArguments, result.Arguments);
}

#endregion Identity / sequence / HiLo

#region PostgreSQL extensions
Expand Down
Loading