From d8f006930c0f9cfe5f1b1f536fb375c20206cef6 Mon Sep 17 00:00:00 2001 From: Jiri Cincura Date: Tue, 10 Dec 2024 15:26:18 +0100 Subject: [PATCH] FbValueGeneratorSelector --- .../Internal/FbValueGeneratorSelector.cs | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/FirebirdSql.EntityFrameworkCore.Firebird/ValueGeneration/Internal/FbValueGeneratorSelector.cs b/src/FirebirdSql.EntityFrameworkCore.Firebird/ValueGeneration/Internal/FbValueGeneratorSelector.cs index dd01a34dc..4e01ee43b 100644 --- a/src/FirebirdSql.EntityFrameworkCore.Firebird/ValueGeneration/Internal/FbValueGeneratorSelector.cs +++ b/src/FirebirdSql.EntityFrameworkCore.Firebird/ValueGeneration/Internal/FbValueGeneratorSelector.cs @@ -44,17 +44,30 @@ public FbValueGeneratorSelector(ValueGeneratorSelectorDependencies dependencies, public new virtual IFbValueGeneratorCache Cache => (IFbValueGeneratorCache)base.Cache; + [Obsolete("Use TrySelect and throw if needed when the generator is not found.")] public override ValueGenerator Select(IProperty property, ITypeBase entityType) + { + if (TrySelect(property, entityType, out var generator)) + { + return generator; + } + + throw new ArgumentException( + CoreStrings.InvalidValueGeneratorFactoryProperty( + nameof(FbSequenceValueGeneratorFactory), property.Name, property.DeclaringType.DisplayName())); + } + + public override bool TrySelect(IProperty property, ITypeBase entityType, out ValueGenerator valueGenerator) { if (property.GetValueGeneratorFactory() != null || property.GetValueGenerationStrategy() != FbValueGenerationStrategy.HiLo) { - return base.Select(property, entityType); + return base.TrySelect(property, entityType, out valueGenerator); } var propertyType = property.ClrType.UnwrapNullableType().UnwrapEnumType(); - var generator = _sequenceFactory.TryCreate( + valueGenerator = _sequenceFactory.TryCreate( property, propertyType, Cache.GetOrAddSequenceState(property, _connection), @@ -62,16 +75,16 @@ public override ValueGenerator Select(IProperty property, ITypeBase entityType) _rawSqlCommandBuilder, _commandLogger); - if (generator != null) + if (valueGenerator != null) { - return generator; + return true; } var converter = property.GetTypeMapping().Converter; if (converter != null && converter.ProviderClrType != propertyType) { - generator = _sequenceFactory.TryCreate( + valueGenerator = _sequenceFactory.TryCreate( property, converter.ProviderClrType, Cache.GetOrAddSequenceState(property, _connection), @@ -79,15 +92,14 @@ public override ValueGenerator Select(IProperty property, ITypeBase entityType) _rawSqlCommandBuilder, _commandLogger); - if (generator != null) + if (valueGenerator != null) { - return generator.WithConverter(converter); + valueGenerator = valueGenerator.WithConverter(converter); + return true; } } - throw new ArgumentException( - CoreStrings.InvalidValueGeneratorFactoryProperty( - nameof(FbSequenceValueGeneratorFactory), property.Name, property.DeclaringType.DisplayName())); + return false; } protected override ValueGenerator FindForType(IProperty property, ITypeBase entityType, Type clrType)