简体   繁体   English

实体框架核心 Linq 查询无法转换为正确的 SQL 语句

[英]Entity Framework Core Linq query doesn't translate to correct SQL statements

So I am debugging our application which uses Entity Framework Core 2.1.14.所以我正在调试我们使用 Entity Framework Core 2.1.14 的应用程序。 I created my entities from database first by scaffolding.我首先通过脚手架从数据库创建了我的实体。 Database is MySQL 5.7.29, the SQL data provider for EF Core is Oracle's MySql.Data.EntityFrameworkCore 8.0.19.数据库是 MySQL 5.7.29,EF Core 的 SQL 数据提供程序是 Oracle 的 MySql.Data.EntityFrameworkCore 8.0.19。

Example: a simple Linq query should create a proper SQL statement in the backend.示例:一个简单的 Linq 查询应该在后端创建一个正确的 SQL 语句。 However it does not.然而事实并非如此。 "Inactive" is a Byte?. “非活动”是一个字节?

Dim associates = (From a In DatabaseContext.Associate
                  Where a.Inactive = 0
                  Select a.AssociateId).ToList()

This simple Linq query returns: SELECT `a`.`Inactive`, `a`.`AssociateID` FROM `associate` AS `a` .这个简单的 Linq 查询返回: SELECT `a`.`Inactive`, `a`.`AssociateID` FROM `associate` AS `a` So basically it downloads all associates and does the "where" in memory and returns the values.所以基本上它会下载所有关联并在 memory 中执行“位置”并返回值。 I know according to the documentation some queries can be complicated and can't be translated, but this is a simplest of the simple queries.我知道根据文档,一些查询可能很复杂并且无法翻译,但这是最简单的简单查询。 Is it because the Inactive column is nullable?是因为 Inactive 列可以为空吗? Something is terrible not right.有什么可怕的不对。

FYI: EF Core is in a class library that is C#.仅供参考:EF Core 位于 class 库中,即 C#。 The VB code is in another project that is VB that references the class library. VB 代码位于另一个引用 class 库的 VB 项目中。

Hmm, seems the answer is to make the query more specific in VB.嗯,似乎答案是使查询在 VB 中更具体。 I guess the query conversion between C# class library and VB project seems to be the issue.我猜 C# class 库和 VB 项目之间的查询转换似乎是问题所在。

Dim associates = (From a In DatabaseContext.Associate
                  Where a.Inactive.Value = Convert.ToByte(0)
                  Select a.AssociateId).ToList()

This provides the correct query:这提供了正确的查询:

SELECT `a`.`AssociateID`
FROM `associate` AS `a`
WHERE `a`.`Inactive` = 0

Very weird that in C# you don't have to check against the.Value and convert the 0 to a byte but in VB I had to do it, there was no other way to get it work.很奇怪,在 C# 中,您不必检查 .Value 并将 0 转换为字节,但在 VB 中我必须这样做,没有其他方法可以让它工作。 Thanks for everyone trying to help with this.感谢所有试图帮助解决此问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM