Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with .Contains() on enum property with a custom ValueConverter #1796

Closed
Xayton opened this issue Apr 12, 2021 · 1 comment · Fixed by #1797
Closed

Problem with .Contains() on enum property with a custom ValueConverter #1796

Xayton opened this issue Apr 12, 2021 · 1 comment · Fixed by #1797
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Xayton
Copy link

Xayton commented Apr 12, 2021

Hello, while using Npgsql.EntityFrameworkCore.PostgreSQL version 5.0.0, I noticed a problem when using a custom ValueConverter on a property of type enum:

public class Blog
{
    public int Id { get; set; }
    public BlogType Type { get; set; } // This enum is converted using a custom ValueConverter.
}

public enum BlogType
{
    Public,    // Converted to "P".
    Internal   // Converted to "I".
}

// Converts the enum to a one-character string. This is just a simplified example.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var converter = new ValueConverter<BlogType, string>(
       v => v == BlogType.Public ? "P" : "I",
       v => v == "P" ? BlogType.Public: BlogType.Internal);

    modelBuilder.Entity<Blog>().Property(e => e.Type).HasConversion(converter);
}

Given the configuration above, the problem happens when querying the blogs with .Contains() on the Type property:

var requiredTypes = new[] {BlogType.Public, BlogType.Internal}; // Must be an array to repeat the problem.
var blogs = _context.Blogs.Where(b => requiredTypes.Contains(b.Type)).ToArray();

Generated SQL query:

Executed DbCommand (11ms) [Parameters=[@__requiredTypes_0='System.String[]' (DbType = Object)], CommandType='Text']
SELECT b.id, b.type
FROM blogs AS b
WHERE b.type = ANY (@__requiredTypes_0)

Here the contents of the @__requiredTypes_0 parameter are not visible (see #1779), but the array contains the string representation of the enum values, instead of the one-character strings provided by the ValueConverter.
So the parameter contains {Public, Internal} instead of {P,I} and consequently no blog results are returned.

May be related to: #1559.

Thank you.

roji added a commit that referenced this issue Apr 12, 2021
roji added a commit that referenced this issue Apr 12, 2021
roji added a commit that referenced this issue Apr 12, 2021
@roji roji self-assigned this Apr 12, 2021
@roji roji added the bug Something isn't working label Apr 12, 2021
@roji roji added this to the 6.0.0 milestone Apr 12, 2021
@roji
Copy link
Member

roji commented Apr 12, 2021

@Xayton thanks for reporting this - I've verified this as a bug and fixed it in #1797. Unfortunately this let into a rabbit hole with two other issues, and the whole thing is a bit much for backporting to a 5.0 patch release...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants