Class ValueComparer<T>
- 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 copy when taking a snapshot. For example, arrays of primitive types will require both if mutation is to be detected.
public class ValueComparer<T> : ValueComparer, IEqualityComparer, IEqualityComparer<object>, IEqualityComparer<T>
Type Parameters
T
The type.
- Inheritance
-
ValueComparer<T>
- 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(bool)
Creates a new ValueComparer<T> with a default comparison expression and a shallow copy for the snapshot.
public ValueComparer(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.
ValueComparer(Expression<Func<T?, T?, bool>>, Expression<Func<T, int>>)
Creates a new ValueComparer<T> with the given comparison expression. A shallow copy will be used for the snapshot.
public ValueComparer(Expression<Func<T?, T?, bool>> equalsExpression, Expression<Func<T, int>> hashCodeExpression)
Parameters
equalsExpression
Expression<Func<T, T, bool>>The comparison expression.
hashCodeExpression
Expression<Func<T, int>>The associated hash code generator.
ValueComparer(Expression<Func<T?, T?, bool>>, Expression<Func<T, int>>, Expression<Func<T, T>>)
Creates a new ValueComparer<T> with the given comparison and snapshotting expressions.
public ValueComparer(Expression<Func<T?, T?, bool>> equalsExpression, Expression<Func<T, int>> hashCodeExpression, Expression<Func<T, T>> snapshotExpression)
Parameters
equalsExpression
Expression<Func<T, T, bool>>The comparison expression.
hashCodeExpression
Expression<Func<T, int>>The associated hash code generator.
snapshotExpression
Expression<Func<T, T>>The snapshot expression.
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.
Properties
EqualsExpression
The comparison expression.
public virtual Expression<Func<T?, T?, bool>> EqualsExpression { get; }
Property Value
- Expression<Func<T, T, bool>>
HashCodeExpression
The hash code expression.
public virtual Expression<Func<T, int>> HashCodeExpression { get; }
Property Value
- Expression<Func<T, int>>
SnapshotExpression
The snapshot expression.
public virtual Expression<Func<T, T>> SnapshotExpression { get; }
Property Value
- Expression<Func<T, T>>
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 override Type Type { get; }
Property Value
Methods
CreateDefaultEqualsExpression()
Creates an expression for equality.
protected static Expression<Func<T?, T?, bool>> CreateDefaultEqualsExpression()
Returns
- Expression<Func<T, T, bool>>
The equality expression.
CreateDefaultHashCodeExpression(bool)
Creates an expression for generating a hash code.
protected static Expression<Func<T, int>> CreateDefaultHashCodeExpression(bool favorStructuralComparisons)
Parameters
favorStructuralComparisons
boolIf true, then IStructuralEquatable is used if the type implements it.
Returns
- Expression<Func<T, int>>
The hash code expression.
CreateDefaultSnapshotExpression(bool)
Creates an expression for creating a snapshot of a value.
protected static Expression<Func<T, T>> CreateDefaultSnapshotExpression(bool favorStructuralComparisons)
Parameters
favorStructuralComparisons
bool
Returns
- Expression<Func<T, T>>
The snapshot expression.
Equals(object?, object?)
Compares the two instances to determine if they are equal.
public override bool Equals(object? left, object? right)
Parameters
Returns
Equals(T?, T?)
Compares the two instances to determine if they are equal.
public virtual bool Equals(T? left, T? right)
Parameters
left
TThe first instance.
right
TThe second instance.
Returns
GetHashCode(object?)
Returns the hash code for the given instance.
public override int GetHashCode(object? instance)
Parameters
instance
objectThe instance.
Returns
- int
The hash code.
GetHashCode(T)
Returns the hash code for the given instance.
public virtual int GetHashCode(T instance)
Parameters
instance
TThe instance.
Returns
- int
The hash code.
Snapshot(object?)
Creates a snapshot of the given instance.
public override 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.
Snapshot(T)
Creates a snapshot of the given instance.
public virtual T Snapshot(T instance)
Parameters
instance
TThe instance.
Returns
- T
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.