Skip to content

Need discussion of Value Comparers #1986

Closed

Description

@IndigoHealth

So, I got this surprisingly helpful warning message, telling me that my POCO property has a value converter but no value comparer. I had followed the guidance on this page and declared a value converter, but it took a lot of digging to come up with this SO answer that showed how to declare and wire up a value comparer.

The SO answer includes a bunch of undocumented magic, in particular propertyBuilder.Metadata.SetValueConverter and propertyBuilder.Metadata.SetValueComparer.

Since creation of a value converter apparently implies the necessity of creating a value comparer, please discuss this subject in this article.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Activity

  1. ajcvickers commented on Dec 12, 2019

    @ajcvickers
    Contributor

    Part of #614

  2. ajcvickers commented on Dec 12, 2019

    @ajcvickers
    Contributor

    Oops. That was for converters. Agree we need docs here.

  3. self-assigned this
    on Dec 12, 2019
  4. added this to the 3.1.0 milestone on Dec 13, 2019
  5. SidShaytay commented on Dec 16, 2019

    @SidShaytay

    Also shouldn't there be a default comparer for each primitive type? We have a custom string -> string converter but we still want a string comparer at the end of the day.

    Edit: To add, the framework should realize this and use those default comparers instead of raising a warning.

  6. ajcvickers commented on Dec 16, 2019

    @ajcvickers
    Contributor

    @SidShetye There is. EF does use the default. But the default may not be correct.

  7. dansoper commented on Jan 9, 2020

    @dansoper

    Is this the same as #1680? That doesn't seem to be linked to this page, though.

  8. ajcvickers commented on Jan 15, 2020

    @ajcvickers
    Contributor

    See also #1680. Make sure this relates to collection conversions.

  9. SidShaytay commented on Jan 30, 2020

    @SidShaytay

    @ajcvickers till the documentation comes up, is this the correct approach for ValueComparers to avoid warnings with custom converters?

    string

    property.SetValueConverter(new MyConverter<string>());
    property.SetValueComparer(new ValueComparer<string>(
    	(l, r) => string.Equals(l, r, StringComparison.Ordinal),
    	v => StringComparer.Ordinal.GetHashCode(v),
    	v => v)
    );
    

    and

    byte[]

    property.SetValueConverter(new MyConverter<byte[]>());
    property.SetValueComparer(new ValueComparer<byte[]>(
    	(l, r) => (l == null || r == null) ? (l == r) : l.SequenceEqual(r),
    	v => v.GetHashCode())
    );
    

    where property is Microsoft.EntityFrameworkCore.Metadata.IMutableProperty and the .SetValueConverter(), .SetValueComparer() methods come from Microsoft.EntityFrameworkCore.MutablePropertyExtensions

  10. 7 remaining items

  11. added 5 commits that reference this issue on Mar 19, 2020
    0250294
    b402547
    33b0518
    2c40e54
    fc76581
  12. added a commit that references this issue on Mar 20, 2020
    2864d29
  13. ajcvickers commented on Mar 20, 2020

    @ajcvickers
    Contributor

    Still need to update the fwlink.

  14. JimInNC commented on Apr 8, 2021

    @JimInNC

    I am hunting the internet for a good explanation of using the ValueConverter in my EF Core Db Context. Following is a snippet from my context. Is this snippet supposed to convert UTC from the database in to Local, and convert Local to UTC when SaveChanges() to the database?

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                 ValueConverter<DateTime, DateTime> dateTimeConverter = new ValueConverter<DateTime, DateTime>(
                   v => v.ToLocalTime(),
                   v => DateTime.SpecifyKind(v, DateTimeKind.Utc));
    
                ValueConverter<DateTime?, DateTime?> nullableDateTimeConverter = new ValueConverter<DateTime?, DateTime?>(
                    v => v.HasValue ? v.Value.ToLocalTime() : v,
                    v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : v);
    
                foreach (var entityType in modelBuilder.Model.GetEntityTypes())
                {
                    if (entityType.IsKeyless)
                    {
                        continue;
                    }
    
                    foreach (var property in entityType.GetProperties())
                    {
                        if (property.ClrType == typeof(DateTime))
                        {
                            property.SetValueConverter(dateTimeConverter);
    
                            property.SetValueComparer(new ValueComparer<DateTime>(
                                (l, r) => l == r,
                                v => StringComparer.Ordinal.GetHashCode(v),
                                v => v)
                            );
                        }
                        else if (property.ClrType == typeof(DateTime?))
                        {
                            property.SetValueConverter(nullableDateTimeConverter);
    
                            property.SetValueComparer(new ValueComparer<DateTime?>(
                               (l, r) => l == r,
                               v => StringComparer.Ordinal.GetHashCode(v),
                               v => v)
                           );
                        }
                    }
                }
  15. ajcvickers commented on Apr 8, 2021

    @ajcvickers
    Contributor

    @JimInNC We have docs with many examples and sample code that can be downloaded from GitHub.

  16. JimInNC commented on Apr 8, 2021

    @JimInNC
  17. weitzhandler commented on Jul 4, 2025

    @weitzhandler
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions