EF Core 3.19.80 Using Any And FirstOrDefault Generates Wrong Sql
See original GitHub issueHi,
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:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Closing as DB 11.2 is generally out of support.