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

Parameter for collection of enum values contains ints even when correlated column has strings #30921

Closed
Tracked by #30731
ajcvickers opened this issue May 18, 2023 · 5 comments
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ajcvickers
Copy link
Member

using (var context = new SomeDbContext())
{
    context.Database.EnsureDeleted();
    context.Database.EnsureCreated();

    var terrains = new[] { Terrain.River, Terrain.Beach, Terrain.Park };
    var walksWithTerrain = await context.Walks
        .Where(e => terrains.Contains(e.Terrain))
        .ToListAsync();
}

public class DogWalk
{
    public int Id { get; set; }
    public Terrain Terrain { get; set; }
}

public enum Terrain
{
    Forest,
    River,
    Hills,
    Village,
    Park,
    Beach,
}

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    public DbSet<DogWalk> Walks => Set<DogWalk>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DogWalk>().Property(e => e.Terrain).HasConversion<string>();
    }
}

Parameter value below should be ["River", "Beach", "Park"]:

info: 5/18/2023 10:32:12.104 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
      Executed DbCommand (40ms) [Parameters=[@__terrains_0='[1,5,4]' (Size = 4000)], CommandType='Text', CommandTimeout='30']
      SELECT [w].[Id], [w].[Terrain]
      FROM [Walks] AS [w]
      WHERE EXISTS (
          SELECT 1
          FROM OpenJson(@__terrains_0) AS [t]
          WHERE [t].[value] = [w].[Terrain])
@roji
Copy link
Member

roji commented May 25, 2023

I looked into it, and the type mapping is correctly inferred from e.Terrain and applied to the parameter collection. However, we currently just hand the collection to JsonSerializer within CollectionToJsonStringConverter - we currently effectively ignore the element type mapping when writing the parameter.

When JSON serialization/deserialization is properly implemented (#30677), we'll build up the parameter JSON string with that, at which point everything should be fine.

We can keep this open to remember to test this specific scenario, or just close it as a dup.

@be-marks
Copy link

Just want to raise a concern as early as possible here - this bug changes the output of .Contains queries we wrote as early as EF Core 5. This prevents us from using 8 until fixed but we're at least aware of the bug - if this goes out as-is in GA many users could upgrade and be unaware

#30677 is currently tagged "type-enhancement" and "needs-design". We're already at preview 4 so I'm nervous that this is a prerequisite - especially if this is closed as a dup

Thanks!

@roji
Copy link
Member

roji commented May 25, 2023

@be-marks we absolutely intend to get this working before 8.0 GA.

@roji
Copy link
Member

roji commented May 26, 2023

@be-marks also, just to make sure that you're aware: you can configure the SQL compatibility level to an old level (as per the blog post) to make EF switch back to the older SQL. This should allow you to continue using previews even before we fix this (the compatibility level doesn't currently affect anything else).

@ajcvickers ajcvickers self-assigned this Jun 28, 2023
@ajcvickers
Copy link
Member Author

Verified fixed:

info: 9/8/2023 13:08:58.838 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) 
      Executed DbCommand (29ms) [Parameters=[@__terrains_0='["River","Beach","Park"]' (Size = 4000)], CommandType='Text', CommandTimeout='30']
      SELECT [w].[Id], [w].[Terrain]
      FROM [Walks] AS [w]
      WHERE [w].[Terrain] IN (
          SELECT [t].[value]
          FROM OPENJSON(@__terrains_0) WITH ([value] nvarchar(max) '$') AS [t]
      )

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 8, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0, 8.0.0-rc2 Sep 8, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0-rc2, 8.0.0 Nov 14, 2023
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
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. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants