gpt4 book ai didi

c# - 转换方法。 "The specified method on the type cannot be translated into a LINQ to Entities store expression"

转载 作者:行者123 更新时间:2023-11-29 14:03:05 25 4
gpt4 key购买 nike

我已将实体 6.0 项目从 SQL Server 迁移到 PostGreSQL。使用 SQL Server,这些对我的查询的转换过去可以正常工作

Module.cs

return (
from m in objDB.Modules
orderby m.ID
select new
{
ID = m.ID,
Name = m.Name,
Status = DB_Function.Convert.ToInt32( m.Status )
}
);

PS: Status是boolean类型

DB_Function.cs

[System.Data.Entity.DbFunctionAttribute( "Business.Database", "ToInt32" )]
public static Int32 ToInt32( Boolean Val )
{
return System.Convert.ToInt32( Val );
}

但是,当我迁移到 PostgreSQL(并因此更改了我的 EDMX)时,这些类型的转换不再执行:

The specified method 'Int32 ToInt32(Boolean)' on the type 'DB_Function+Convert' cannot be translated into a LINQ to Entities store expression.

这个错误与 PostGre 有关(比如 int4 而不是 int32)或者我遗漏了什么?

提前致谢。

最佳答案

要了解我们可以在 LINQ to Enitities 中使用什么类型的函数:请浏览以下链接: linq to entities doesn't recognize a method

现在在这种特殊情况下,我们在 LINQ to Entites 中有一个函数,它无法转换为 SQL 查询,因此抛出了异常。因此,通过删除该功能,它可以完美地工作。

返回( 来自 objDB.Modules 中的 m orderby m.ID 选择新的 { ID = m.ID, 姓名 = m.姓名, 状态 = m.Status == 真? 1 : 0 } );

关于c# - 转换方法。 "The specified method on the type cannot be translated into a LINQ to Entities store expression",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30782788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com