Table of Contents

Class ValueConverter

Namespace
Microsoft.EntityFrameworkCore.Storage.ValueConversion
Assembly
Microsoft.EntityFrameworkCore.dll

Defines conversions from an object of one type in a model to an object of the same or different type in the store.

public abstract class ValueConverter
Inheritance
ValueConverter
Derived
Inherited Members

Remarks

See EF Core value converters for more information and examples.

Constructors

ValueConverter(LambdaExpression, LambdaExpression, ConverterMappingHints?)

Initializes a new instance of the ValueConverter class.

protected ValueConverter(LambdaExpression convertToProviderExpression, LambdaExpression convertFromProviderExpression, ConverterMappingHints? mappingHints = null)

Parameters

convertToProviderExpression LambdaExpression

The expression to convert objects when writing data to the store, exactly as supplied and may not handle nulls, boxing, and non-exact matches of simple types.

convertFromProviderExpression LambdaExpression

The expression to convert objects when reading data from the store, exactly as supplied and may not handle nulls, boxing, and non-exact matches of simple types.

mappingHints ConverterMappingHints

Hints that can be used by the ITypeMappingSource to create data types with appropriate facets for the converted data.

Remarks

See EF Core value converters for more information and examples.

ValueConverter(LambdaExpression, LambdaExpression, bool, ConverterMappingHints?)

Initializes a new instance of the ValueConverter class, allowing conversion of nulls.

Warning: this is currently an internal API since converting nulls to and from the database can lead to broken queries and other issues. See GitHub issue #26230 for more information and examples.

[EntityFrameworkInternal]
protected ValueConverter(LambdaExpression convertToProviderExpression, LambdaExpression convertFromProviderExpression, bool convertsNulls, ConverterMappingHints? mappingHints = null)

Parameters

convertToProviderExpression LambdaExpression

The expression to convert objects when writing data to the store, exactly as supplied and may not handle nulls, boxing, and non-exact matches of simple types.

convertFromProviderExpression LambdaExpression

The expression to convert objects when reading data from the store, exactly as supplied and may not handle nulls, boxing, and non-exact matches of simple types.

convertsNulls bool

If true, then the nulls will be passed to the converter for conversion. Otherwise null values always remain null.

mappingHints ConverterMappingHints

Hints that can be used by the ITypeMappingSource to create data types with appropriate facets for the converted data.

Remarks

See EF Core value converters for more information and examples.

Properties

ConvertFromProvider

Gets the function to convert objects when reading data from the store, setup to handle nulls, boxing, and non-exact matches of simple types.

public abstract Func<object?, object?> ConvertFromProvider { get; }

Property Value

Func<object, object>

Remarks

See EF Core value converters for more information and examples.

ConvertFromProviderExpression

Gets the expression to convert objects when reading data from the store, exactly as supplied and may not handle nulls, boxing, and non-exact matches of simple types.

public virtual LambdaExpression ConvertFromProviderExpression { get; }

Property Value

LambdaExpression

Remarks

See EF Core value converters for more information and examples.

ConvertToProvider

Gets the function to convert objects when writing data to the store, setup to handle nulls, boxing, and non-exact matches of simple types.

public abstract Func<object?, object?> ConvertToProvider { get; }

Property Value

Func<object, object>

Remarks

See EF Core value converters for more information and examples.

ConvertToProviderExpression

Gets the expression to convert objects when writing data to the store, exactly as supplied and may not handle nulls, boxing, and non-exact matches of simple types.

public virtual LambdaExpression ConvertToProviderExpression { get; }

Property Value

LambdaExpression

Remarks

See EF Core value converters for more information and examples.

ConvertsNulls

If true, then the nulls will be passed to the converter for conversion. Otherwise null values always remain null.

public virtual bool ConvertsNulls { get; }

Property Value

bool

Remarks

By default, value converters do not handle nulls so that a value converter for a non-nullable property (such as a primary key) can be used for correlated nullable properties, such as any corresponding foreign key properties.

See EF Core value converters for more information and examples.

MappingHints

Hints that can be used by the ITypeMappingSource to create data types with appropriate facets for the converted data.

public virtual ConverterMappingHints? MappingHints { get; }

Property Value

ConverterMappingHints

Remarks

See EF Core value converters for more information and examples.

ModelClrType

The CLR type used in the EF model.

public abstract Type ModelClrType { get; }

Property Value

Type

Remarks

See EF Core value converters for more information and examples.

ProviderClrType

The CLR type used when reading and writing from the store.

public abstract Type ProviderClrType { get; }

Property Value

Type

Remarks

See EF Core value converters for more information and examples.

Methods

CheckTypeSupported(Type, Type, params Type[])

Checks that the type used with a value converter is supported by that converter and throws if not.

protected static Type CheckTypeSupported(Type type, Type converterType, params Type[] supportedTypes)

Parameters

type Type

The type to check.

converterType Type

The value converter type.

supportedTypes Type[]

The types that are supported.

Returns

Type

The given type.

ComposeWith(ValueConverter?)

Composes another ValueConverter instance with this one such that the result of the first conversion is used as the input to the second conversion.

public virtual ValueConverter ComposeWith(ValueConverter? secondConverter)

Parameters

secondConverter ValueConverter

The second converter.

Returns

ValueConverter

The composed converter.

Remarks

See EF Core value converters for more information and examples.