Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and 返回

SqlSugar 处理完成
3 492

一个很简单的SQL查询语句报错
Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '='

查询的方法如下:
_db.Queryable<SysCodeGenConfig>() .Where(u => u.CodeGenId == input.CodeGenId && u.WhetherCommon != YesNoEnum.Y.ToString()) .Select<CodeGenConfig>() .Mapper(u => { u.NetType = (u.EffectType == "EnumSelector" || u.EffectType == "ConstSelector" ? u.DictTypeCode : u.NetType); }) .OrderBy(u => u.OrderNo)


生成的SQL如下:
SELECT IdASId ,CodeGenIdASCodeGenId ,ColumnNameASColumnName ,PropertyNameASPropertyName ,ColumnLengthASColumnLength ,ColumnCommentASColumnComment ,NetTypeASNetType ,EffectTypeASEffectType ,FkEntityNameASFkEntityName ,FkTableNameASFkTableName ,FkColumnNameASFkColumnName ,FkColumnNetTypeASFkColumnNetType ,DictTypeCodeASDictTypeCode ,WhetherRetractASWhetherRetract ,WhetherRequiredASWhetherRequired ,QueryWhetherASQueryWhether ,QueryTypeASQueryType ,WhetherTableASWhetherTable ,WhetherAddUpdateASWhetherAddUpdate ,ColumnKeyASColumnKey ,DataTypeASDataType ,WhetherCommonASWhetherCommon ,DisplayColumnASDisplayColumn ,ValueColumnASValueColumn ,PidColumnASPidColumn ,OrderNoASOrderNoFROMSysCodeGenConfig WHERE ((CodeGenId= @CodeGenId0 ) AND (WhetherCommon<> CAST(@MethodConst1 AS CHAR)))ORDER BYOrderNoASC

执行时报错:
"Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '<>'"

如果将u.WhetherCommon != YesNoEnum.Y.ToString() 修改为 u.WhetherCommon != "Y" ,则可以避免这个问题

var query = _db.Queryable<SysCodeGenConfig>() .Where(u => u.CodeGenId == input.CodeGenId && u.WhetherCommon != "Y") .Select<CodeGenConfig>() .Mapper(u => { u.NetType = (u.EffectType == "EnumSelector" || u.EffectType == "ConstSelector" ? u.DictTypeCode : u.NetType); }) .OrderBy(u => u.OrderNo);

修改后生成的SQL语句如下:
SELECTIdASId ,CodeGenIdASCodeGenId ,ColumnNameASColumnName ,PropertyNameASPropertyName ,ColumnLengthASColumnLength ,ColumnCommentASColumnComment ,NetTypeASNetType ,EffectTypeASEffectType ,FkEntityNameASFkEntityName ,FkTableNameASFkTableName ,FkColumnNameASFkColumnName ,FkColumnNetTypeASFkColumnNetType ,DictTypeCodeASDictTypeCode ,WhetherRetractASWhetherRetract ,WhetherRequiredASWhetherRequired ,QueryWhetherASQueryWhether ,QueryTypeASQueryType ,WhetherTableASWhetherTable ,WhetherAddUpdateASWhetherAddUpdate ,ColumnKeyASColumnKey ,DataTypeASDataType ,WhetherCommonASWhetherCommon ,DisplayColumnASDisplayColumn ,ValueColumnASValueColumn ,PidColumnASPidColumn ,OrderNoASOrderNoFROMSysCodeGenConfig WHERE ((CodeGenId= @CodeGenId0 ) AND (WhetherCommon<> @WhetherCommon1 ))ORDER BYOrderNoASC


我运行环境是.netcore 8.0 正式版,sqlsugar 5.1.4.118-preview02,mysql 8.0
请问应该如何修复?


一句话就是,linq里的

Where(u => u.WhetherCommon != YesNoEnum.Y.ToString())
会转成 以下语句导致异常
WHERE (WhetherCommon<> CAST(@MethodConst1 AS CHAR))

热忱回答3

  • fate sta fate sta VIP0
    2023/11/19

    tostring 提取到外面操作

    0 回复
  • fate sta fate sta VIP0
    2023/11/19

    image.png

    0 回复
  • fate sta fate sta VIP0
    2023/11/19

    这个是编码类型和cast as char 转换不兼容


    tostring 提取到外面操作


    var myString=yyy.ToString();

    Where(x=>x.yyy=myString)

    0 回复