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

标签 c# linq entity-framework postgresql

我已将实体 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/

相关文章:

c# - WPF 全局异常处理程序

c# - 方法返回 xmldocument 的单元测试

c# - 使用 Linq 按不同分组

c# - 如何使用 Linq 处理带有 FirstOrDefault 的 NULL 对象属性

c# - 如何将 POCO 层与 Entity Framework 层分离,并且仍然能够向对象添加业务逻辑

entity-framework - 如何防止Json.NET使用 Entity Framework 代理类型名称?

c# - 从 linq to xml 中只选择一个随机节点

c# - 获取所有打开的 WPF 窗口

c# - linq - 字符串列表 - 仅按字母排序,然后按数字排序

c# - Linq查询OrderBy多个