question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Core 3.19.80 Using Any And FirstOrDefault Generates Wrong Sql

See original GitHub issue

Hi,

i have just upgraded all packages in my solutions and i have a strange bug. Some of my queries have exists filter. If i build the query like below, EF core generates 2 where statement in sql command text.

Query:

 IQueryable<TableEntity> query = unitOfWork.Context.Set<TableEntity>();
query = query.Where(q => unitOfWork.Context.GlEntityUsers.Any(t => t.RestrictedFlag == 0 && t.EntityCode == q.ChargeEntityCode));
query = query.Where(p => p.TransactionNo == TransactionNo);
var q1 = query.FirstOrDefault();

Generated Sql Where Statement:

WHERE (((EXISTS (
    SELECT `1`
    FROM "AVLOWN"."GL_ENTITY_USERS" "g2"
    WHERE (((("g2"."RESTRICTED_FLAG" = 0)) AND (("g2"."ENTITY_CODE" = "b"."CHARGE_ENTITY_CODE")))))) AND (("b"."TRANSACTION_NO" = :TransactionNo_0))))
where rownum <= 1 

If i remove Any filter, efcore generates correct command text.

 IQueryable<TableEntity> query = unitOfWork.Context.Set<TableEntity>();
query = query.Where(p => p.TransactionNo == TransactionNo);
var q1 = query.FirstOrDefault();

Generated Sql Where Statement:

WHERE ("b"."TRANSACTION_NO" = :TransactionNo_0)
and rownum <= 1 

is this a bug or am i doing something wrong?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
alexkehcommented, Oct 28, 2020

I’ve reproduced the error. I filed bug 32081523 to track the issue and have a dev team member review it.

The SQL generated is related to Oracle DB 11.2 SQL syntax. If you change the SQL compatibility level from “11” to any DB server level higher, it will generate working SQL for your app. It’s the 11g type syntax that happens to be incorrectly generated. This is a workaround if you aren’t actually using an 11.2 DB.

0reactions
alexkehcommented, Dec 26, 2022

Closing as DB 11.2 is generally out of support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core generated SQL is quite inefficient in this case, any ...
1 Answer 1 · To get first record. Or same as Single(). · added screenshot. 3x faster. · Actually the first LINQ query...
Read more >
FromSqlRaw FirstOrDefault - Microsoft Q&A
I am using EntityFramework Core in an application. ... can FisrtOrDefault() not be used with FromRawSql or am I donng something else wrong?...
Read more >
EFCore Linq to Entities — sql injection : r/dotnet
FirstOrDefault methods and you haven't injected an interceptor, this isn't vulnerable to an SQL Injection attack. I'll also assume that resut.
Read more >
Untitled
EF Core 3.19.80 Using Any And FirstOrDefault Generates Wrong Sql ... 2020 · ToQueryString() method helps us to see database queries generated by...
Read more >
Querying data via the DbSet
The EF Core provider that you use is responsible for translating the LINQ query into the actual SQL to be executed against the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found