Convert "The LINQ expression could not be translated" warning into an error

c# entity-framework-core

Question

I was looking at my logs recently, and I noticed that one of my queries was causing a warning that said "The LINQ expression '{expression}' could not be translated and will be evaluated locally." This is because of a mistake that I made in my query expression.

I was able to fix this issue without too much trouble. My question is: How can I detect this situation in the future without manually looking at the logs?

Two possible solutions (neither of which I know how to implement):

  • Change the warning into an error
  • Write a test that detects the problem. I currently have tests that use an in memory database to test other aspects of the query.

My goal is to be able to prevent a mistake like this from accidentally slipping into production.

1
0
7/22/2018 2:36:39 AM

Accepted Answer

Using the link provided by @GertArnold, I was able to convert the warning into an error using the following code:

services.AddDbContext<MyContext>(
    options =>
    {
        options.UseSqlServer("connection-string");
        options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
    }
)
3
10/19/2018 8:48:25 PM


Related Questions





Related

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