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
LambdaExpressionThe comparison expression.
hashCodeExpression
LambdaExpressionThe associated hash code generator.
snapshotExpression
LambdaExpressionThe 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
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
Properties
EqualsExpression
The comparison expression.
public virtual LambdaExpression EqualsExpression { get; }
Property Value
HashCodeExpression
The hash code expression.
public virtual LambdaExpression HashCodeExpression { get; }
Property Value
SnapshotExpression
The snapshot expression.
public virtual LambdaExpression SnapshotExpression { get; }
Property Value
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
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
Returns
CreateDefault(Type, bool)
Creates a default ValueComparer<T> for the given type.
public static ValueComparer CreateDefault(Type type, bool favorStructuralComparisons)
Parameters
type
TypeThe type.
favorStructuralComparisons
boolIf true, then EF will use IStructuralEquatable if the type implements it. This is usually used when byte arrays act as keys.
Returns
CreateDefault<T>(bool)
Creates a default ValueComparer<T> for the given type.
public static ValueComparer CreateDefault<T>(bool favorStructuralComparisons)
Parameters
favorStructuralComparisons
boolIf true, then EF will use IStructuralEquatable if the type implements it. This is usually used when byte arrays act as keys.
Returns
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
Returns
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
ExpressionThe new left expression.
rightExpression
ExpressionThe 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
ExpressionThe 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
ExpressionThe 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
objectThe instance.
Returns
- int
The hash code.
Snapshot(object?)
Creates a snapshot of the given instance.
public abstract object? Snapshot(object? instance)
Parameters
instance
objectThe 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.