EF Core MySQL 包含(变量)like 运算符
EF Core MySQL Contains(variable) like operator
我正在使用 ASP.NET Web API Core 和 EF Core 创建一个方法,returns JSON 格式化记录。
当我将 contains 用于 like 运算符 (%varible%) 时,出现以下消息的错误:
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: 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 '+ '%'
ORDER BY p
.StoreId
, p
.CategoryId
' at line 4
似乎 % 符号之一不在正确的位置。
我检查了我的代码和原始 sql 输出,但没有发现任何问题。
希望有人能帮助我!
var query = from p in db.Products
join c in db.ProductCategory
on new { p.StoreId, p.CategoryId } equals new { c.StoreId, c.CategoryId } into pc
from c in pc.DefaultIfEmpty()
where (p.ProductName.Contains("a"))
select p;
query.ToList()
原始 SQL
SELECT `p`.`StoreId`, `p`.`ProductId`, `p`.`CategoryId`, `p`.`Description`, `p`.`ImagePath`, `p`.`ProductName`, `p`.`SalesPeriodFrom`, `p`.`SalesPeriodNeverEnd`, `p`.`SalesPeriodTo`, `p`.`SalesUnit`, `p`.`UnitPrice`, `c`.`StoreId`, `c`.`CategoryId`, `c`.`CategoryName`
FROM `Products` AS `p`
LEFT JOIN `ProductCategory` AS `c` ON (`p`.`StoreId` = `c`.`StoreId`) AND (`p`.`CategoryId` = `c`.`CategoryId`)
WHERE `p`.`ProductName` LIKE ('%' + 'a') + '%'
ORDER BY `p`.`StoreId`, `p`.`CategoryId`
Windows 7 专业 x64
Visual Studio 2015 专业
MySQL5.6.17
微软 .NET 核心 1.0.1
有依赖关系
"Microsoft.ApplicationInsights.AspNetCore": "1.0.2",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"MySql.Data.EntityFrameworkCore": "7.0.5-IR21",
"System.Linq.Dynamic.Core": "1.0.6.6"
我发现将 MySql.Data.EntityFrameworkCore 从“7.0.6-IR31”更改为“6.10.0-alpha”会产生有效的 SQL 语法。
6.10.0-alpha 更新。
但是,另一个问题出现了。请看这里
我觉得没办法了,只能等更新修复bug了
我正在使用 ASP.NET Web API Core 和 EF Core 创建一个方法,returns JSON 格式化记录。
当我将 contains 用于 like 运算符 (%varible%) 时,出现以下消息的错误:
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: 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 '+ '%'
ORDER BY
p
.StoreId
,p
.CategoryId
' at line 4
似乎 % 符号之一不在正确的位置。
我检查了我的代码和原始 sql 输出,但没有发现任何问题。
希望有人能帮助我!
var query = from p in db.Products
join c in db.ProductCategory
on new { p.StoreId, p.CategoryId } equals new { c.StoreId, c.CategoryId } into pc
from c in pc.DefaultIfEmpty()
where (p.ProductName.Contains("a"))
select p;
query.ToList()
原始 SQL
SELECT `p`.`StoreId`, `p`.`ProductId`, `p`.`CategoryId`, `p`.`Description`, `p`.`ImagePath`, `p`.`ProductName`, `p`.`SalesPeriodFrom`, `p`.`SalesPeriodNeverEnd`, `p`.`SalesPeriodTo`, `p`.`SalesUnit`, `p`.`UnitPrice`, `c`.`StoreId`, `c`.`CategoryId`, `c`.`CategoryName`
FROM `Products` AS `p`
LEFT JOIN `ProductCategory` AS `c` ON (`p`.`StoreId` = `c`.`StoreId`) AND (`p`.`CategoryId` = `c`.`CategoryId`)
WHERE `p`.`ProductName` LIKE ('%' + 'a') + '%'
ORDER BY `p`.`StoreId`, `p`.`CategoryId`
Windows 7 专业 x64
Visual Studio 2015 专业
MySQL5.6.17
微软 .NET 核心 1.0.1
有依赖关系
"Microsoft.ApplicationInsights.AspNetCore": "1.0.2",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"MySql.Data.EntityFrameworkCore": "7.0.5-IR21",
"System.Linq.Dynamic.Core": "1.0.6.6"
我发现将 MySql.Data.EntityFrameworkCore 从“7.0.6-IR31”更改为“6.10.0-alpha”会产生有效的 SQL 语法。
6.10.0-alpha 更新。
但是,另一个问题出现了。请看这里
我觉得没办法了,只能等更新修复bug了