-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Entity Framework Core 3.1 Enum Conversion failed when converting the nvarchar value 'EnumValue' to data type int #20534
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
After some analysis it looks like there is a bug in expression formation. I can see my coding breaking in EF Core 3.1.3 where as it was working in EF Core 2.2.6. I'm forming the expression dynamically in the run time and that works with EF Core 2.2.6 but breaks in EF Core 3.1.3. I'll share the expression formed in both version here in a day or two |
Here is what I have got.
But using Is this is the expected behavior? I need to use |
@fingers10 I'm not really following your comments about the expression building--are you referring to expressions EF is building, the compiler is building, or that you are building manually? Looking at your original code, mapping the enum to a string is working for me. Note that you should still use the enum value in the query--EF will convert it to a string via the value converter. public class DemoEntity
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Position Position { get; set; }
}
public enum Position
{
[Display(Name = "Accountant")]
Accountant,
[Display(Name = "Chief Executive Officer (CEO)")]
ChiefExecutiveOfficer,
[Display(Name = "Integration Specialist")]
IntegrationSpecialist,
[Display(Name = "Junior Technical Author")]
JuniorTechnicalAuthor,
[Display(Name = "Pre Sales Support")]
PreSalesSupport,
[Display(Name = "Sales Assistant")]
SalesAssistant,
[Display(Name = "Senior Javascript Developer")]
SeniorJavascriptDeveloper,
[Display(Name = "Software Engineer")]
SoftwareEngineer
}
public static class Program
{
public static void Main()
{
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.AddRange(
new DemoEntity { Position = Position.IntegrationSpecialist },
new DemoEntity { Position = Position.Accountant }
);
context.SaveChanges();
}
using (var context = new SomeDbContext())
{
var test = context
.Set<DemoEntity>()
.Where(x => x.Position == Position.Accountant)
.ToList();
Console.WriteLine(test[0].Position);
}
}
}
public class SomeDbContext : DbContext
{
private static readonly ILoggerFactory
Logger = LoggerFactory.Create(x => x.AddConsole()); //.SetMinimumLevel(LogLevel.Debug));
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DemoEntity>().Property(e => e.Position).HasConversion<string>();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(Logger)
.EnableSensitiveDataLogging()
.UseSqlServer(Your.SqlServerConnectionString);
} |
@ajcvickers I'm referring to the expression formed by EF Core. In your above code, you're using |
@fingers10 Thanks! @smitpatel Enum mapped to a string column. This works: var test = context
.Set<DemoEntity>()
.Where(x => x.Position == Position.Accountant)
.ToList(); This throws: var test = context
.Set<DemoEntity>()
.Where(x => x.Position.Equals(Position.Accountant))
.ToList();
|
@ajcvickers You're welcome. I'm happy that I'm able to contribute by finding issues. |
I'm using Entity Framework Core 3.1 and trying to do a simple query on an enum property in my entity in my localdb and I keep getting this error:
Entity:
Enum - Position:
DbContext:
I tried adding value conversions:
.HasConversion<string>()
- didn't work.HasConversion(converter)
- didn't workWhen I query the table as follows I'm getting the conversion error
The
Position
is of typeNVARCHAR(MAX)
in my database.I modified the query to
query.Where(x => x.Position.Equals(Position.Accountant.ToString())).ToListAsync()
. Now the query executes but I get zero results.Expected Result:
I should get one record from the database.
The text was updated successfully, but these errors were encountered: