-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Cannot query on converted enum property #18489
Comments
Likely fixed in 3.0 |
I do not have an option of going to 3.0, it is not backwards compatible with netstandard2.0/Full framework 4.8 |
It was reported fixed/closed in 2.1 but it not working |
Update And filtering by Status2 also results in "could not be translated and will be evaluated locally"
This is what the Eval is attempting Why is it trying to convert to ASCII code? I also attempted
results in Any suggestions or workarounds? |
If my previous comment is relevant is this the issue we should be looking at? What would the solution be? is this going to be back-ported to EF Core 2 ? |
@freemstr I think the main issue here (at least in 3.0; this won't get fixed for a 2.x release) is that value converters don't support converting from null to some other value--see #13850. This means that s => s == 'X' ? EventStatus.Booked : EventStatus.Free will never get called with null, and instead an attempt will be made to set the property to null. Things work for two non-null values: public class Event
{
public int Id { get; set; }
public EventStatus Status { get; set; }
}
public enum EventStatus { Booked = 0, Free = 1}
public class BloggingContext : DbContext
{
private readonly ILoggerFactory Logger = LoggerFactory.Create(c => c.AddConsole());
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseLoggerFactory(Logger)
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Event>()
.Property(e => e.Status)
.HasMaxLength(1)
.HasConversion(new ValueConverter<EventStatus, char?>(
i => i == EventStatus.Booked ? (char?) 'X' : 'Y',
s => s == 'X' ? EventStatus.Booked : EventStatus.Free
));
}
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.AddRange(
new Event(),
new Event {Status = EventStatus.Booked},
new Event {Status = EventStatus.Free});
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var r1 = context.Set<Event>().Where(e => e.Status == EventStatus.Booked).ToList();
var r2 = context.Set<Event>().Where(e => e.Status == EventStatus.Free).ToList();
}
}
} |
Thank you for your time and detailed explanation
But I am glad that #13850 is still open and looking forward to seeing it implemented. |
I am using https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions for documentation however
I am getting
I should be able to get a WHERE clause
[messageRecord].Status = 'X'
Steps to reproduce
In the DB Status column is char(1) with either 'X' or NULL (3rd party legacy code)
Further technical details
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.2.6-servicing-10079 initialized '****' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: NoTracking SensitiveDataLoggingEnabled
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'where (Convert([messageRecord].Status) == Convert(__eventStatus_0))' could not be translated and will be evaluated locally.
EF Core version:2.2.6-servicing-10079
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: netstandard2.0
Operating system: Win 10 64bit
IDE: Visual Studio 2019 16.3.5
The text was updated successfully, but these errors were encountered: