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
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ public override bool TrySelect(IProperty property, ITypeBase typeBase, out Value
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override ValueGenerator? FindForType(IProperty property, ITypeBase typeBase, Type clrType)
=> property.ClrType.UnwrapNullableType() == typeof(Guid)
? property.ValueGenerated == ValueGenerated.Never || property.GetDefaultValueSql() is not null
? new TemporaryGuidValueGenerator()
: new NpgsqlSequentialGuidValueGenerator()
: base.FindForType(property, typeBase, clrType);
=> property.ClrType.UnwrapNullableType() switch
{
var t when t == typeof(Guid) && property.ValueGenerated is not ValueGenerated.Never && property.GetDefaultValueSql() is null
=> new NpgsqlSequentialGuidValueGenerator(),

var t when t == typeof(string) && property.ValueGenerated is not ValueGenerated.Never && property.GetDefaultValueSql() is null
=> new NpgsqlSequentialStringValueGenerator(),

_ => base.FindForType(property, typeBase, clrType)
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Npgsql.EntityFrameworkCore.PostgreSQL.ValueGeneration;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class NpgsqlSequentialStringValueGenerator : ValueGenerator<string>
{
private readonly NpgsqlSequentialGuidValueGenerator _guidGenerator = new();

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public override bool GeneratesTemporaryValues => false;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public override string Next(EntityEntry entry) => _guidGenerator.Next(entry).ToString();
}
4 changes: 2 additions & 2 deletions test/EFCore.PG.Tests/NpgsqlValueGeneratorSelectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Returns_built_in_generators_for_types_setup_for_value_generation()
AssertGenerator<TemporaryShortValueGenerator>("NullableShort");
AssertGenerator<TemporaryByteValueGenerator>("NullableByte");
AssertGenerator<TemporaryDecimalValueGenerator>("Decimal");
AssertGenerator<StringValueGenerator>("String");
AssertGenerator<NpgsqlSequentialStringValueGenerator>("String");
AssertGenerator<NpgsqlSequentialGuidValueGenerator>("Guid");
AssertGenerator<BinaryValueGenerator>("Binary");
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public void Returns_sequence_value_generators_when_configured_for_model()
AssertGenerator<NpgsqlSequenceHiLoValueGenerator<int>>("NullableInt", setSequences: true);
AssertGenerator<NpgsqlSequenceHiLoValueGenerator<long>>("NullableLong", setSequences: true);
AssertGenerator<NpgsqlSequenceHiLoValueGenerator<short>>("NullableShort", setSequences: true);
AssertGenerator<StringValueGenerator>("String", setSequences: true);
AssertGenerator<NpgsqlSequentialStringValueGenerator>("String", setSequences: true);
AssertGenerator<NpgsqlSequentialGuidValueGenerator>("Guid", setSequences: true);
AssertGenerator<BinaryValueGenerator>("Binary", setSequences: true);
}
Expand Down
Loading