efcore: Enum inequality comparison triggers query warning
Comparing two enums via inequality within a SQL query causes EF Core to report a warning like this:
Microsoft.EntityFrameworkCore.Query:Warning: A SQL parameter or literal was generated for the type ‘Color’ using the ValueConverter ‘EnumToNumberConverter<Color, int>’. Review the generated SQL for correctness and consider evaluating the target expression in-memory instead.
The generated SQL is fine:
SELECT [b].[Id], [b].[Color]
FROM [Boxes] AS [b]
[b].[Color] >= 4
If the comparison is equality, the warning does not appear.
Repro:
enum Color { Red, Orange, Yellow, Green, Blue, Indigo, Violet}
class Box
{
public int Id { get; set; }
public Color Color { get; set; }
}
class BoxContext : DbContext
{
public DbSet<Box> Boxes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Doesn't matter. The warning appears before attempting to connect.");
optionsBuilder.UseLoggerFactory(debugLoggerFactory);
}
static readonly LoggerFactory debugLoggerFactory = new LoggerFactory(new[] { new DebugLoggerProvider((_, __) => true) });
}
public class Program
{
public static void Main(string[] args)
{
var db = new BoxContext();
var bluishBoxes = db.Boxes.Where(b => b.Color >= Color.Blue).ToArray();
}
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 20 (9 by maintainers)
@johnduhart Yes, from the comments by @smitpatel further down the issue indicate that it was removed because the query changes made the warning unnecessary.
@ajcvickers Gotcha, but that’s global…is there a way to turn it off on the piece of code throwing it, as that’s what I’ve verified as working? Disabling it globally when it’s a per-instance thing isn’t how most warnings work, take C# itself for example and
#pragma
.Agreed, the resolution doesn’t make sense here. The issue isn’t the warning really, it’s the inconsistency. There is no warning for a normal equals (or contains), only for not equals and not contains. There should be warnings for both, or warnings for neither.
@ajcvickers It looks like this specific warning was removed in 3.0, did it get replaced with something else? #12085
As far as I can tell: yes.
@breyed I agree that this is quite annoying, hopefully it will be looked at soon - I’ve experienced the same warning with negated
Contains
call. Meanwhile you can disable the warning:The problem is that the warning exists at all, i.e. it’s a spurious warning that requires the developer to track down what’s going on, only to discover that his code and the generated SQL are both just fine and no action was actually required.
There is no warning in the similar case of equality. You could say that the inconsistency between the behavior of equality and inequality is a problem, but I’d just say that the case of equality is working and inequality has a subtle bug. There shouldn’t be a warning in either case.