Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -379,40 +379,15 @@ public static void Create(
if (constructor == null
|| jsonReaderWriterProperty == null)
{
AddNamespace(typeof(ValueConverter<,>), parameters.Namespaces);
AddNamespace(converter.ModelClrType, parameters.Namespaces);
AddNamespace(converter.ProviderClrType, parameters.Namespaces);

var unsafeAccessors = new HashSet<string>();

mainBuilder
.Append("new ValueConverter<")
.Append(codeHelper.Reference(converter.ModelClrType))
.Append(", ")
.Append(codeHelper.Reference(converter.ProviderClrType))
.AppendLine(">(")
.IncrementIndent()
.AppendLines(
codeHelper.Expression(converter.ConvertToProviderExpression, parameters.Namespaces, unsafeAccessors),
skipFinalNewline: true)
.AppendLine(",")
.AppendLines(
codeHelper.Expression(converter.ConvertFromProviderExpression, parameters.Namespaces, unsafeAccessors),
codeHelper.Expression(converter.ConstructorExpression, parameters.Namespaces, unsafeAccessors),
Comment thread
AndriySvyryd marked this conversation as resolved.
Outdated
skipFinalNewline: true);

Check.DebugAssert(
unsafeAccessors.Count == 0, "Generated unsafe accessors not handled: " + string.Join(Environment.NewLine, unsafeAccessors));

if (converter.ConvertsNulls)
{
mainBuilder
.AppendLine(",")
.Append("convertsNulls: true");
}

mainBuilder
.Append(")")
.DecrementIndent();
}
else
{
Expand Down Expand Up @@ -549,7 +524,15 @@ public static void CreateJsonValueReaderWriter(
return;
}

CreateJsonValueReaderWriter(jsonValueReaderWriterType, parameters, codeHelper);
var unsafeAccessors = new HashSet<string>();
Comment thread
AndriySvyryd marked this conversation as resolved.
Outdated

mainBuilder
.AppendLines(
codeHelper.Expression(jsonValueReaderWriter.ConstructorExpression, parameters.Namespaces, unsafeAccessors),
skipFinalNewline: true);

Check.DebugAssert(
unsafeAccessors.Count == 0, "Generated unsafe accessors not handled: " + string.Join(Environment.NewLine, unsafeAccessors));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Storage/ValueConversion/ValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class ValueConverter
{
internal static readonly ConstructorInfo MappingHintsCtor
= typeof(ConverterMappingHints).GetConstructor(
[typeof(int?), typeof(int?), typeof(int?), typeof(bool?), typeof(Func<IProperty, IEntityType, ValueGenerator>)])!;
[typeof(int?), typeof(int?), typeof(int?), typeof(bool?)])!;

/// <summary>
/// Initializes a new instance of the <see cref="ValueConverter" /> class.
Expand Down
40 changes: 30 additions & 10 deletions src/EFCore/Storage/ValueConversion/ValueConverter`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,40 @@ public override Type ProviderClrType
typeof(ConverterMappingHints)
])!;

private readonly ConstructorInfo _constructorInfoWithConvertsNulls = typeof(ValueConverter<TModel, TProvider>).GetConstructor(
[
typeof(Expression<Func<TModel, TProvider>>),
typeof(Expression<Func<TProvider, TModel>>),
typeof(bool),
typeof(ConverterMappingHints)
])!;

/// <inheritdoc />
public override Expression ConstructorExpression
=> Expression.New(
_constructorInfo,
ConvertToProviderExpression,
ConvertFromProviderExpression,
MappingHints != null
? Expression.New(
{
get
{
var mappingHintsExpression = MappingHints != null
? (Expression)Expression.New(
MappingHintsCtor,
Expression.Constant(MappingHints.Size, typeof(int?)),
Expression.Constant(MappingHints.Precision, typeof(int?)),
Expression.Constant(MappingHints.Scale, typeof(int?)),
Expression.Constant(MappingHints.IsUnicode, typeof(bool?)),
// valueGeneratorFactory is difficult to build using Expression trees and is obsolete
Expression.Default(typeof(Func<IProperty, IEntityType, ValueGenerator>)))
: Expression.Default(typeof(ConverterMappingHints)));
Expression.Constant(MappingHints.IsUnicode, typeof(bool?)))
: Expression.Default(typeof(ConverterMappingHints));

return ConvertsNulls
? Expression.New(
_constructorInfoWithConvertsNulls,
ConvertToProviderExpression,
ConvertFromProviderExpression,
Expression.Constant(true),
mappingHintsExpression)
: Expression.New(
_constructorInfo,
ConvertToProviderExpression,
ConvertFromProviderExpression,
mappingHintsExpression);
}
}
}
Loading
Loading