IQueryable 创建查询失败,并出现错误 升级到 .net 6 和 ef 6 后,无法将类型为“System.Object”的表达式用于参数

IQueryable create query fails with error Expression of type 'System.Object' cannot be used for parameter after upgrading to .net 6 and ef 6

提问人:SpyrosT 提问时间:9/26/2023 最后编辑:SpyrosT 更新时间:9/26/2023 访问量:64

问:

我正在调用一大块代码 `

var m = typeof(IQueryProvider).GetMethods().First(x => x.Name == "CreateQuery" && x.IsGenericMethod); 
var gm = m.MakeGenericMethod(queryType);
var a = (IQueryable)gm.Invoke(provider, new object[] { expression });

提供商来自哪里


    (from p in Context.Set<PointOfInterest>() select p).Provider

查询类型为 PointOfInterestDto

表达式是

.Call System.Linq.Queryable.Where(
    .Call System.Linq.Queryable.Select(
        .Extension<Microsoft.EntityFrameworkCore.Query.QueryRootExpression>,
        '(.Lambda #Lambda1<System.Func`2[BibeCoffee.Logic.PointsOfInterest.Domain.PointOfInterestAggregate.PointOfInterest,BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto]>))
    ,
    '(.Lambda #Lambda2<System.Func`2[BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto,System.Boolean]>))

.Lambda #Lambda1<System.Func`2[BibeCoffee.Logic.PointsOfInterest.Domain.PointOfInterestAggregate.PointOfInterest,BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto]>(BibeCoffee.Logic.PointsOfInterest.Domain.PointOfInterestAggregate.PointOfInterest $pointOfInterest)
{
    .New BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto(){
        Name = $pointOfInterest.Name,
        Id = $pointOfInterest.Id,
        ExtraFields = (System.Collections.Generic.IDictionary`2[System.String,System.Object])$pointOfInterest.ExtraFields
    }
}

.Lambda #Lambda2<System.Func`2[BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto,System.Boolean]>(BibeCoffee.Logic.PointsOfInterest.DtoModels.PointOfInterestDto $o)
{
    .Call System.Linq.Enumerable.Any(
        (System.Collections.Generic.IEnumerable`1[System.Object]).Call ($o.ExtraFields).get_Item("coffeeChannels"),
        .Lambda #Lambda3<System.Func`2[System.Object,System.Boolean]>)
}

.Lambda #Lambda3<System.Func`2[System.Object,System.Boolean]>(System.Object $o) {
    .Call ((System.Collections.Generic.IDictionary`2[System.String,System.Object])$o).ContainsKey("id") && (System.Guid).Call ((System.Collections.Generic.IDictionary`2[System.String,System.Object])$o).get_Item("id")
    == .Constant<System.Guid>(d5471682-df51-480b-a367-35b7a918f172)
}

调用后,可查询对象会给出这种错误

Expression of type 'System.Object' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[System.Object]' of method 'System.Linq.IQueryable`1[System.Object] AsQueryable[Object](System.Collections.Generic.IEnumerable`1[System.Object])' (Parameter 'arg0')

请记住,extrafields 属性是一个 IDictionary<string, object> ,在这种情况下,coffeeChannel 属性有一个 elemets 数组,我想在其中搜索。 此外,这在以前的版本(ef5、.net5)上也工作正常

我在 sym 表中的所有库中都放置了一个调试器,以逐步了解 CreateQuery 内部发生的事情,但 VS 无法介入。

我仔细检查了表情,看起来不错。

我交叉检查了几个重大更改,但没有运气(也许我错过了一些东西)。

asp.net Linq net-6.0

评论

0赞 Svyatoslav Danyliv 9/26/2023
其他代码在哪里?从未尝试过动态调用。此外,安装 ReadableExpressions.Visualizers 以使用表达式简化生活。CreateQuery
0赞 SpyrosT 9/26/2023
其他代码是什么意思?这是有问题的一点。
0赞 Svyatoslav Danyliv 9/26/2023
有很多变量,初始化是未知的:、、。我无法读取调试表达式输出,但可以在表达式构建代码中找到错误。还有你想构建什么?providerqueryTypeexpression
0赞 SpyrosT 9/26/2023
给出了表达式,我将用其余的更新帖子。我正在使用一个自定义库(用于以前的版本),该库使用 OData 标准过滤数据库。
0赞 Panagiotis Kanavos 9/26/2023
这段代码试图做什么?OData 不需要此类代码。无论它多么古怪(我也有一个不幸的想法,将 OData 与 EF Core 一起使用),它都不需要反射来生成模型或查询。生成 EDM 模型可能很古怪,但 OData 使用反射来查找 DbContext 中配置的类型和关系

答: 暂无答案