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

Enum ICollection Contains => TRUE = FALSE #23059

Closed
kevinvenclovas opened this issue Oct 20, 2020 · 7 comments · Fixed by #24732
Closed

Enum ICollection Contains => TRUE = FALSE #23059

kevinvenclovas opened this issue Oct 20, 2020 · 7 comments · Fixed by #24732
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@kevinvenclovas
Copy link

kevinvenclovas commented Oct 20, 2020

Hello,

i have a bug or need some help with my code.

await context.Users.Where(x => !x.IsSoftDeleted && (x.MessageGroups.Contains(group) || x.MessageGroups.Contains(MessageGroup.ALL))).ToListAsync();

With this code EfCore generate this SQL:

SELECT ... FROM .. AS a
      WHERE NOT (a."IsSoftDeleted") AND ((TRUE = FALSE) OR (TRUE = FALSE))

So why the Contains generate TRUE = FALSE ?

Databasefield has this Conversion:
entity.Property(e => e.MessageGroups).HasConversion(v => string.Join(',', v), v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => x.ToEnum<MessageGroup>()).ToList());

Can someone help me. Thanks!

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 20, 2020

Which provider and EF Core version?

@kevinvenclovas
Copy link
Author

kevinvenclovas commented Oct 20, 2020

@ErikEJ
Npgsql.EntityFrameworkCore.PostgreSQL => 3.1.4
Microsoft.EntityFrameworkCore => 3.1.9

@ajcvickers
Copy link
Member

/cc @roji for PostgreSQL

@ajcvickers
Copy link
Member

Possible dupe of #20721, but waiting for @roji to take a look from the PostgreSQL side.

@smitpatel
Copy link
Member

Not a dupe of #20721 (no property access in the query). Dupe of #10434

@roji
Copy link
Member

roji commented Oct 25, 2020

As far as I can tell, there's nothing PostgreSQL-specific in the code above - the enum array/list is value-converted to a database string field (i.e. no usage of native PostgreSQL enums).

However, if you use Npgsql.EntityFrameworkCore.PostgreSQL 5.0.0-rc2 and remove the value conversion, everything will work just fine:

await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

var group = MessageGroup.SomeGroup;
_ = await ctx.Users
    .Where(x => !x.IsSoftDeleted && (x.MessageGroups.Contains(group) || x.MessageGroups.Contains(MessageGroup.ALL)))
    .ToListAsync();

public class BlogContext : DbContext
{
    public DbSet<User> Users { get; set; }

    static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(@"Host=localhost;Username=test;Password=test")
            .EnableSensitiveDataLogging()
            .UseLoggerFactory(ContextLoggerFactory);
}

public class User
{
    public int Id { get; set; }
    public bool IsSoftDeleted { get; set; }
    public List<MessageGroup> MessageGroups { get; set; }
}

public enum MessageGroup
{
    ALL,
    SomeGroup
}

SQL:

SELECT u."Id", u."IsSoftDeleted", u."MessageGroups"
FROM "Users" AS u
WHERE NOT (u."IsSoftDeleted") AND (u."MessageGroups" @> ARRAY[@__group_0]::integer[] OR u."MessageGroups" @> ARRAY[0]::integer[])

@ajcvickers ajcvickers added this to the 6.0.0 milestone Oct 26, 2020
smitpatel added a commit that referenced this issue Apr 22, 2021
@smitpatel
Copy link
Member

On main branch, I am not seeing any such SQL. We accurately generate translation failure error as we cannot translate Contains over a string column in database.

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 22, 2021
@ghost ghost closed this as completed in #24732 Apr 22, 2021
ghost pushed a commit that referenced this issue Apr 22, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-preview5 Apr 26, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-preview5, 6.0.0 Nov 8, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants