I'm using C# to write LINQ in to a MySQL database. I think the SQL generated might be wrong for a simple table join that I'm doing.
My nuget packages are Mysql.Data v6.9.9, Mysql.data.entities v6.8.3, and MySql.data.entity v6.9.9
The LINQ is this:
query = from peopleResult in query
join t in technologyQuery on peopleResult.Company_Id equals t.Company_Id
select peopleResult;
The SQL generated looks like this:
SELECT ...
FROM `people` AS `Extent1`
INNER JOIN `technologies` AS `Extent2` ON (`Extent1`.`Company_Id` = `Extent2`.`Company_Id`) OR ((`Extent1`.`Company_Id` IS NULL) AND (`Extent2`.`Company_Id` IS NULL))
WHERE ...
Is this part of the join right?
(`Extent1`.`Company_Id` IS NULL) AND (`Extent2`.`Company_Id` IS NULL)
The query is incredibly long running when that is included. I pulled that out of the SQL with a regex, and it runs much faster and seems to give the correct results.
Is my LINQ incorrect or missing something? Does the MySQL linq-to-sql likely have a bug?
Thank you for your time thinking about this.
It's not a MySQL connector bug, but EF feature which tries to emulate the C# equality rules for nullable types.
First, make sure to set DbContext.Configuration.UseDatabaseNullSemantics
to true
, for instance inside your DbContext
derived class constructor:
Configuration.UseDatabaseNullSemantics = true;
By idea this should solve the issue. However they implemented it for comparison operators and forgot the joins. So you have to use the alternative join syntax with where
clause:
query =
from peopleResult in query
from t in technologyQuery
where peopleResult.Company_Id == t.Company_Id
select peopleResult;
which will be translated to the desired SQL JOIN
without IS NULL
part.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.