Table of Contents

Class ValueComparer

Namespace
Microsoft.EntityFrameworkCore.ChangeTracking
Assembly
Microsoft.EntityFrameworkCore.dll

Specifies custom value snapshotting and comparison for CLR types that cannot be compared with Equals(object, object) and/or need a deep/structural copy when taking a snapshot. For example, arrays of primitive types will require both if mutation is to be detected.

public abstract class ValueComparer : IEqualityComparer, IEqualityComparer<object>
Inheritance
ValueComparer
Implements
Derived
Inherited Members
Extension Methods

Remarks

Snapshotting is the process of creating a copy of the value into a snapshot so it can later be compared to determine if it has changed. For some types, such as collections, this needs to be a deep copy of the collection rather than just a shallow copy of the reference.

See EF Core value comparers for more information and examples.

Constructors

ValueComparer(LambdaExpression, LambdaExpression, LambdaExpression)

Creates a new ValueComparer with the given comparison and snapshotting expressions.

protected ValueComparer(LambdaExpression equalsExpression, LambdaExpression hashCodeExpression, LambdaExpression snapshotExpression)

Parameters

equalsExpression LambdaExpression

The comparison expression.

hashCodeExpression LambdaExpression

The associated hash code generator.

snapshotExpression LambdaExpression

The snapshot expression.

Fields

BoolIdentity

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

[EntityFrameworkInternal]
protected static readonly Expression<Func<bool, bool>> BoolIdentity

Field Value

Expression<Func<bool, bool>>

HashCodeAddMethod

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

[EntityFrameworkInternal]
protected static readonly MethodInfo HashCodeAddMethod

Field Value

MethodInfo

ToHashCodeMethod

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

[EntityFrameworkInternal]
protected static readonly MethodInfo ToHashCodeMethod

Field Value

MethodInfo

Properties

EqualsExpression

The comparison expression.

public virtual LambdaExpression EqualsExpression { get; }

Property Value

LambdaExpression

HashCodeExpression

The hash code expression.

public virtual LambdaExpression HashCodeExpression { get; }

Property Value

LambdaExpression

SnapshotExpression

The snapshot expression.

public virtual LambdaExpression SnapshotExpression { get; }

Property Value

LambdaExpression

Remarks

Snapshotting is the process of creating a copy of the value into a snapshot so it can later be compared to determine if it has changed. For some types, such as collections, this needs to be a deep copy of the collection rather than just a shallow copy of the reference.

Type

The type.

public abstract Type Type { get; }

Property Value

Type

Methods

Add(HashCode, int)

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

[EntityFrameworkInternal]
public static HashCode Add(HashCode hash, int code)

Parameters

hash HashCode
code int

Returns

HashCode

CreateDefault(Type, bool)

Creates a default ValueComparer<T> for the given type.

public static ValueComparer CreateDefault(Type type, bool favorStructuralComparisons)

Parameters

type Type

The type.

favorStructuralComparisons bool

If true, then EF will use IStructuralEquatable if the type implements it. This is usually used when byte arrays act as keys.

Returns

ValueComparer

The ValueComparer<T>.

CreateDefault<T>(bool)

Creates a default ValueComparer<T> for the given type.

public static ValueComparer CreateDefault<T>(bool favorStructuralComparisons)

Parameters

favorStructuralComparisons bool

If true, then EF will use IStructuralEquatable if the type implements it. This is usually used when byte arrays act as keys.

Returns

ValueComparer

The ValueComparer<T>.

Type Parameters

T

The type.

Equals(object?, object?)

Compares the two instances to determine if they are equal.

public abstract bool Equals(object? left, object? right)

Parameters

left object

The first instance.

right object

The second instance.

Returns

bool

true if they are equal; false otherwise.

ExtractEqualsBody(Expression, Expression)

Takes EqualsExpression and replaces the two parameters with the given expressions, returning the transformed body.

public virtual Expression ExtractEqualsBody(Expression leftExpression, Expression rightExpression)

Parameters

leftExpression Expression

The new left expression.

rightExpression Expression

The new right expression.

Returns

Expression

The body of the lambda with left and right parameters replaced.

ExtractHashCodeBody(Expression)

Takes the HashCodeExpression and replaces the parameter with the given expression, returning the transformed body.

public virtual Expression ExtractHashCodeBody(Expression expression)

Parameters

expression Expression

The new expression.

Returns

Expression

The body of the lambda with the parameter replaced.

ExtractSnapshotBody(Expression)

Takes the SnapshotExpression and replaces the parameter with the given expression, returning the transformed body.

public virtual Expression ExtractSnapshotBody(Expression expression)

Parameters

expression Expression

The new expression.

Returns

Expression

The body of the lambda with the parameter replaced.

GetHashCode(object?)

Returns the hash code for the given instance.

public abstract int GetHashCode(object? instance)

Parameters

instance object

The instance.

Returns

int

The hash code.

Snapshot(object?)

Creates a snapshot of the given instance.

public abstract object? Snapshot(object? instance)

Parameters

instance object

The instance.

Returns

object

The snapshot.

Remarks

Snapshotting is the process of creating a copy of the value into a snapshot so it can later be compared to determine if it has changed. For some types, such as collections, this needs to be a deep copy of the collection rather than just a shallow copy of the reference.