Closed DotCat1985 closed 2 years ago
@lauxjpn - Thoughts?
@Tokyo1985 You are using Oracle's EF Core provider, so I can't comment specifically here, since I am the maintainer of Pomelo.EntityFrameworkCore.MySql.
That being said, it looks like Oracle's provider does not translate OUTER APPLY
to anything meaningful at all (the OUTER APPLY
clause is not valid SQL for MySQL and is just what EF Core generates by default, if a provider does not handle it specifically).
In Pomelo.EntityFrameworkCore.MySql
, OUTER APPLY
is being translated to LATERAL
for MySQL databases, which is supported since MySQL 8.0.14
. With Pomelo, you can explicitly set your database server version, and you would not get a syntax error, but an exception telling you, that your database version is not compatible with the OUTER APPLY
statement.
But most importantly, with Pomelo and MySQL 8.0.14+, your query would run.
If you cannot upgrade your database server, you have the following options:
OUTER APPLY
/ LATERAL
statement. This can usually be achieved by using includes where possible, instead of subqueries.If you can switch to Pomelo, but cannot upgrade your database server, you have the additional option to:
5.0.0-alpha.2
, rewrite your query to use includes (and filters) and use the new Split Queries of EF Core 5.@lauxjpn - Sorry, my mistake, I did not see it was different MySql provider.
@Tokyo1985 @lauxjpn Notice that ef core 5 split query does not support subqueries. Thus I don't think split query can help.
@Tokyo1985 To use an available ef core query, as @lauxjpn said, dto objects should be built on the client side. Another choice is to use raw sql, to query for a dto which is not an entity type, see https://github.com/dotnet/efcore/issues/1862#issuecomment-597022290
@Tokyo1985 You are using Oracle's EF Core provider, so I can't comment specifically here, since I am the maintainer of Pomelo.EntityFrameworkCore.MySql.
That being said, it looks like Oracle's provider does not translate
OUTER APPLY
to anything meaningful at all (theOUTER APPLY
clause is not valid SQL for MySQL and is just what EF Core generates by default, if a provider does not handle it specifically).In
Pomelo.EntityFrameworkCore.MySql
,OUTER APPLY
is being translated toLATERAL
for MySQL databases, which is supported since MySQL8.0.14
. With Pomelo, you can explicitly set your database server version, and you would not get a syntax error, but an exception telling you, that your database version is not compatible with theOUTER APPLY
statement. But most importantly, with Pomelo and MySQL 8.0.14+, your query would run.If you cannot upgrade your database server, you have the following options:
* Rewrite your query, that it does not have to use an `OUTER APPLY` / `LATERAL` statement. This can usually be achieved by using includes where possible, instead of subqueries. * Manually split the query into parts, which you then query individually, before combining their results into your DTO.
If you can switch to Pomelo, but cannot upgrade your database server, you have the additional option to:
* Use Pomelo `5.0.0-alpha.2`, rewrite your query to use includes (and filters) and use the new [Split Queries](https://docs.microsoft.com/en-us/ef/core/querying/single-split-queries#split-queries-1) of EF Core 5.
Would I get exception with Pomelo and MySQL 5.7?
Am closing this as an external issue that isn't related to EF Core itself (but feel free to continue the conversation, of course!).
Would I get exception with Pomelo and MySQL 5.7?
@Tokyo1985 Yes, you would get an exception (though a different one telling you what the problem is). It's because:
[...]
OUTER APPLY
is being translated toLATERAL
for MySQL databases, which is supported since MySQL 8.0.14 [...] with Pomelo and MySQL 8.0.14+, your query would run.
Hi, I developed the DB context for MySQL 5.7.33 and entity models using Entity Framework Core 3.1.1 Code First
When I execute the following method with nested LINQ queries:
I receive the following exception and stacktrace:
MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUTER APPLY ( SELECT
d
.ID
,d
.Name
,d
.Label
,t0
.Response
' at line 12EF Core version: 3.1.1 Database: MySQL 5.7.33 Database provider: MySql.Data.EntityFrameworkCore 8.0.22 Target framework: .NET Core 3.1.0 Operating system: Windows 10 IDE: Visual Studio 2019 16.8.30907.101
I also verified that Exception does not occur when I use the SQL Server 2019 and Microsoft.EntityFrameworkCore.SqlServer. Is it a bug? If no, how can I fix the issue above? I need a feedback soon. Thank you for attention.