2

I wonder if there is any way to validate a Expression towards EF core without using a real database.

We could of course do the validation in a integration tests, but It would be very helpful if we could ensure that the expression is valid already in a unit test.

To me, a novice, it feels like pretty basic functionality. Despite this I have yet to find any solution when looking around on the internet.

Any insight would be appreciated.

Edit: To summarize, I do not need to validate the sql ( does it find table X etc ) rather I only want to know that EF can generate a query string from the expression tree.

Tobias
  • 21
  • 2
  • You can only validate in a test using the targeted provider. But you can Make that test run really fast. – ErikEJ Dec 09 '22 at 15:54
  • 1
    Just do `query.ToQueryString()` which makes EF translate the expression without executing it. – Gert Arnold Dec 09 '22 at 15:55
  • Entity Framework is an abstraction layer for interacting with a database. What are you having problems with? Writing code? -- Which a unit test can check. Or when EF translates your query to the underlying database provider and then executes it on the database? -- which you won't be able to check without executing it on the actual database. – BurnsBA Dec 09 '22 at 17:55
  • @GertArnold Good idea, but it does not seem to work here; "The given 'IQueryable' does not support generation of query strings." – Tobias Dec 12 '22 at 09:45
  • @BurnsBA I find this strange. At some point before sending the query towards the database EF does a translation of the expression tree into a sql statement. I simply want to do that without an database running. – Tobias Dec 12 '22 at 09:48
  • Printing the generated SQL is a different question from validation. See https://stackoverflow.com/q/1412863/1462295 or https://stackoverflow.com/a/4700400/1462295 – BurnsBA Dec 12 '22 at 14:04
  • @BurnsBA That is fair, I have updated the question – Tobias Dec 14 '22 at 15:13

0 Answers0