diff --git a/src/EFCore.Relational/Storage/RelationalTypeMapping.cs b/src/EFCore.Relational/Storage/RelationalTypeMapping.cs index c84afcbcf64..a245c446b55 100644 --- a/src/EFCore.Relational/Storage/RelationalTypeMapping.cs +++ b/src/EFCore.Relational/Storage/RelationalTypeMapping.cs @@ -572,7 +572,7 @@ public virtual string GenerateSqlLiteral(object? value) { value = NormalizeEnumValue(value); - if (Converter != null && Converter.ProviderClrType.UnwrapNullableType() != value?.GetType()) + if (Converter != null) { value = Converter.ConvertToProvider(value); } diff --git a/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs b/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs index 88d84995cdc..75e4aa8c05f 100644 --- a/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs +++ b/src/EFCore.SqlServer/Query/Internal/SqlServerObjectToStringTranslator.cs @@ -102,22 +102,7 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact if (instance.Type.IsEnum) { - if (instance.TypeMapping?.DbType == DbType.String - || string.Equals(instance.TypeMapping?.StoreTypeNameBase, "VARCHAR", StringComparison.OrdinalIgnoreCase)) - { - return instance; - } - else - { - var cases = Enum.GetValues(instance.Type) - .Cast() - .Select(value => new CaseWhenClause( - _sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(value)), - _sqlExpressionFactory.Constant(value?.ToString(), typeof(string)))) - .ToArray(); - - return _sqlExpressionFactory.Case(cases, _sqlExpressionFactory.Constant(string.Empty, typeof(string))); - } + return _sqlExpressionFactory.Convert(instance, typeof(string)); } return TypeMapping.TryGetValue(instance.Type, out var storeType) diff --git a/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs b/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs index bd4ec8c7f15..5469d53bd9e 100644 --- a/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs +++ b/src/EFCore.Sqlite.Core/Query/Internal/SqliteObjectToStringTranslator.cs @@ -99,27 +99,7 @@ public SqliteObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFactory _sqlExpressionFactory.Constant(true.ToString())); } - if (instance.Type.IsEnum) - { - if (instance.TypeMapping?.DbType == DbType.String - || string.Equals(instance.TypeMapping?.StoreType, "TEXT", StringComparison.OrdinalIgnoreCase)) - { - return instance; - } - else - { - var cases = Enum.GetValues(instance.Type) - .Cast() - .Select(value => new CaseWhenClause( - _sqlExpressionFactory.Equal(instance, _sqlExpressionFactory.Constant(value)), - _sqlExpressionFactory.Constant(value?.ToString(), typeof(string)))) - .ToArray(); - - return _sqlExpressionFactory.Case(cases, _sqlExpressionFactory.Constant(string.Empty, typeof(string))); - } - } - - return TypeMapping.Contains(instance.Type) + return TypeMapping.Contains(instance.Type) || instance.Type.IsEnum ? _sqlExpressionFactory.Convert(instance, typeof(string)) : null; }