EF Core 3.1 doesn't translate with a helper class on querying

c# entity-framework-core


In .NET Core 2.2, I used to use a helper class that decrypts the data from the database and retrieves the user information, but now I can't. I get an exception saying that it can't' be translated.

That's the same logic used in .NET Core 2.2

var cp = await Context.Company
                      .Where(a => Encrypt.Decry(a.Email, a.Em) == login.Email 
                                  && login.Password == Hash.get(a.Password))

The exception :

The LINQ expression 'DbSet.Where(c => Encrypt.Decry(value: c.Email, sec: c.Em) == __login_Email_0 && __login_Password_1 == Hash.get(c.Password))' 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."}

I don't get why I should explicitly used AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync() ?

Update : So I tried to follow the example by adding AsEnumerable but it must be with await and I get an error on where

 var cp = await Context.Company.AsAsyncEnumerable().Where(a => Encrypt.Decry(a.Email, a.Em) == login.Email && login.Password == Hash.get(a.Password)).SingleOrDefaultAsync();
3/22/2020 9:13:41 AM

Accepted Answer

EF Core can't translate Encrypt.Decry to SQL query. That's why you see the error. You can load all data into memory and then execute Encrypt.Decry.

From https://docs.microsoft.com/en-us/ef/core/querying/client-eval:

Prior to version 3.0, Entity Framework Core supported client evaluation anywhere in the query. For more information, see the previous versions section.

Most likely now it's implemented like this to avoid unintentional client evaluation where a lot of data can be loaded into memory. Now you should explicitly configure client evaluation.

3/22/2020 9:13:17 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow