-
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
Using Enum in linq query throws exception in v3.1.4 #20968
Comments
@canertosuner What is the implementation of |
It’s a method in GenericRepo class and the ProviderRepository is inherited from this class.
|
@canertosuner What is the reason for ignoring the navigation property here? modelBuilder.Entity<Provider>()
.Ignore(p => p.Bank); I tried building your model, but it's unclear to me how all the mapping classes you have above work together. It would help us investigate if you could attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing. |
if I don't Ignore it for the base type mapping it throws an exception but it was definetly working on v2.2. I have attached a sample runnable project about the case. It uses inmemorydb as storage in the sample app. Just send a request to the Get method using swagger UI. |
@canertosuner Thanks for the additional info. It seems like the code is using
Client evaluation like this is no longer supported in EF Core 3+. Hence for 3.1 an exception is thrown. As the exception message says, you can get back the 2.x behavior by explicitly adding client evaluation to the query using AsEnumerable, ToList, or similar. Another option is to allow EF to use your public abstract class Provider
{
public Guid Id { get; set; }
public string ClientId { get; set; }
public abstract BankEnum Bank { get; internal set; }
}
public enum BankEnum
{
XBank = 1,
YBank = 2,
}
public class XBankProvider : Provider
{
public string DomainName { get; protected set; }
public override BankEnum Bank
{
get => BankEnum.XBank;
internal set { }
}
}
public class YBankProvider : Provider
{
public string Name { get; protected set; }
public override BankEnum Bank
{
get => BankEnum.YBank;
internal set { }
}
}
public class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Provider>(b =>
{
b.ToTable("Provider");
b.HasDiscriminator(e => e.Bank)
.HasValue<XBankProvider>(BankEnum.XBank)
.HasValue<YBankProvider>(BankEnum.YBank);
});
}
private static readonly ILoggerFactory
Logger = LoggerFactory.Create(x => x.AddConsole()); //.SetMinimumLevel(LogLevel.Debug));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(Logger)
.EnableSensitiveDataLogging()
.UseSqlServer(Your.SqlServerConnectionString);
}
public static class Program
{
public static void Main()
{
using (var context = new MyDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.SaveChanges();
}
using (var context = new MyDbContext())
{
var b = BankEnum.YBank;
var result = context.Set<Provider>().SingleOrDefault(c => c.Bank == b);
}
}
} |
Thank you for that @ajcvickers . I tried the way you suggested and it worked for v3.1 |
I have base and sub classes and their mappings as blow, seperated them using HasDiscriminator method
when I try to make a query like this;
it throws this exception ;
The LINQ expression 'DbSet could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Before v3.x it was working but it's not working after v3.1.
How to solve this problem ?
Further technical details
EF Core version: 3.1.4
Database provider: PostgreSql
Target framework: (e.g. .NET Core 3.1)
Operating system:
IDE: (e.g. Visual Studio 2019 16.3)
The text was updated successfully, but these errors were encountered: