草庐IT

mysql - EF Core MySQL Contains(variable) like operator

coder 2023-10-22 原文

我正在使用 ASP.NET Web API Core 和 EF Core 创建一个方法,它返回 JSON 格式的记录。
当我将 contains 用于 like operator (%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 专业版
MySQL 5.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 更新。

然而,另一个问题出现了。请看这里。

use parameter from method argument inside linq contains

我认为除了等待更新修复错误别无选择。

关于mysql - EF Core MySQL Contains(variable) like operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308449/

有关mysql - EF Core MySQL Contains(variable) like operator的更多相关文章

  1. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  2. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  3. ruby-on-rails - 无法安装 mysql2 0.3.14 gem - 2

    我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby​​目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin

  4. ruby - Chef : Read variable from file and use it in one converge - 2

    我有以下代码,它下载一个文件,然后将文件的内容读入一个变量。使用该变量,它执行一个命令。这个配方不会收敛,因为/root/foo在编译阶段不存在。我可以通过多个聚合和一个来解决这个问题ifFile.exist但我想用一个收敛来完成它。关于如何做到这一点有什么想法吗?execute'download_joiner'docommand"awss3cps3://bucket/foo/root/foo"not_if{::File.exist?('/root/foo')}endpassword=::File.read('/root/foo').chompexecute'join_domain'd

  5. ruby 代码 : why put colon in front of variable name (inside initialize method) - 2

    我遇到了一些Ruby代码,我试图理解为什么变量在initialize方法声明中的名称末尾有冒号。冒号有什么原因吗?attr_reader:var1,:var2definitialize(var1:,var2:)@var1=var1@var2=var2end 最佳答案 那些是关键字参数。您可以按名称而非位置使用它们。例如ThatClass.new(var1:42,var2:"foo")或ThatClass.new(var2:"foo",var1:42)Anarticleaboutkeywordargumentsbythoughtbot

  6. ruby - 混帐 & ruby : How can I unset the GIT_DIR variable from inside a ruby script? - 2

    我编写了一个非常简单的“部署”脚本,作为我的裸git存储库中的post-updateHook运行。变量如下livedomain=~/mydomain.comstagingdomain=~/stage.mydomain.comgitrepolocation=~/git.mydomain.com/thisrepo.git(bare)core=~/git.mydomain.com/thisrepo.gitcore==addedremoteintoeachlive&stagegitslive和stage都初始化了gitrepos(非裸),我已经将我的裸仓库作为远程添加到它们中的每一个(名为co

  7. ruby - 如何使用 ruby​​ mysql2 执行事务 - 2

    我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi

  8. ruby - : mean in rails before a variable name? 是什么 - 2

    例如,:符号-我正在尝试弄清楚:的含义,以及它与@的区别也没有任何符号。如果有真正有用的指南! 最佳答案 它是一个符号,是一种Ruby语言结构。符号类似于字符串,但thisblogpost解释细节。@表示类的实例变量:它基本上是一个在类实例的所有方法之间共享的变量。它与:无关。 关于ruby-:meaninrailsbeforeavariablename?是什么,我们在StackOverflow上找到一个类似的问题: https://stackoverflow

  9. ruby - :variable and @variable 之间的差异 - 2

    作为RubyonRails新手,我明白“@”和“:”引用有不同的含义。我看到了thispost在SO中,其中描述了一些差异。@表示实例变量(例如@my_selection):表示别名(例如:my_selection)我遇到了一个情况,我有一个标准的MVC页面,类似于我的网络应用程序中的所有其他表单/页面。html.erb片段route.rb片段resources:my_selections当我尝试访问此页面时,出现此错误:NoMethodErrorinselections#createShowingC:/somedir/myapp/app/views/my_selections/ind

  10. css - rails 萨斯 : variables are not passed with @import - 2

    我有一个使用twitterbootstrap和sass的Rails元素。scss文件结构化到文件夹中,所以我有更好的概述。现在我想为包含我的颜色等的全局变量定义一个文件,并将这些值传递给其他文件,这样我就有更少的冗余代码。虽然所有代码都已正确导入和应用,变量不起作用。这是当前的设置:样式表/application.css.scss/**=require_self*=require_tree*//*stylesheets/||–base/||–_reset.scss#Reset/normalize||–_typography.scss#Typographyrules||–componen

随机推荐