shuxinqin / Chloe

A lightweight and high-performance Object/Relational Mapping(ORM) library for .NET --C#
https://github.com/shuxinqin/Chloe/wiki
MIT License
1.52k stars 455 forks source link

Does not support method 'Object.Equals(Object objA,Object objB)' #315

Closed ragnaroks closed 2 years ago

ragnaroks commented 2 years ago

entity.Idrequest.Id 都是 Int32,且不小于 1,在此例中分别是 1638516385,这个异常可能是什么原因导致?

Boolean exist;
try {
    exist=databaseContext.Query<Account>(entity=>Sql.Equals(entity.Id,request.Id)).Any();
}catch(Exception exception){
    this.Logger.LogError(exception,"执行数据库查询异常");
    return this.Json(response);
}
System.NotSupportedException: Does not support method 'Object.Equals(Object objA,Object objB)'.
    at Chloe.MySql.SqlGenerator.Visit(DbMethodCallExpression exp)
    at Chloe.MySql.SqlGenerator.BuildWhereState(DbExpression whereExpression)
    at Chloe.MySql.SqlGenerator.BuildGeneralSql(DbSqlQueryExpression exp)
    at Chloe.MySql.SqlGenerator.Visit(DbSqlQueryExpression exp)
    at Chloe.MySql.DbExpressionTranslator.Translate(DbExpression expression)
    at Chloe.Query.Internals.InternalQuery`1.GenerateCommandFactor()
    at Chloe.Query.Internals.InternalQuery`1.GetEnumerator()
    at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
    at HttpApi.Controllers.AppController.CheckBind(DatabaseContext databaseContext, CheckBindRequest request) in D:\repo\HttpApi\Controllers\AppController.cs:line 0
shuxinqin commented 2 years ago

Sql.Equals 是支持的。你这个异常肯定不是 exist=databaseContext.Query(entity=>Sql.Equals(entity.Id,request.Id)).Any(); 这行代码引发的。 Object.Equals 确实是不支持,查看有没有使用 Object.Equals(Object objA,Object objB) 的地方

ragnaroks commented 2 years ago

调试确实是这个地方引发的异常,修改为 Sql.Equals<Int32>(entity.Id,request.Id) 后异常消失

shuxinqin commented 2 years ago

(⊙o⊙)…~有一种情况会出现你这个异常,可能是 entity.Id, request.Id 类型不一致,导致写法是 Sql.Equals,但实际调用了 Object.Equals 方法 确保 entity.Id, request.Id 类型一致就可以解决了

ragnaroks commented 2 years ago

上面有提到 entity.Idrequest.Id 类型都是 Int32,猜测某个地方类型发生了隐式转换,可能从 Int32 变成了 Int16