【问题标题】:How to get return from Postgresql Function with EF in Dotnet Core如何在 Dotnet Core 中使用 EF 从 Postgresql 函数获取返回
【发布时间】:2020-09-11 04:51:28
【问题描述】:

我正在尝试通过 EF 核心在 postgresql 中使用函数和存储过程。我通过编写 ef 迁移文件创建了一个函数并更新了我的数据库。我可以看到我的函数是在 db 上创建的,当我在 Pgadmin 中运行“select public.sp_Get_Products()”时,我得到了正确的结果。

但是当我尝试通过 EF 核心获取结果时,下面的代码会返回给我:-1

_context.Database.ExecuteSqlCommand("select public.sp_Get_Products()");

如果我尝试使用下面的 FromSqlRaw,它会返回 The required column 'Id' was not present in the result of a 'FromSql' operation

_context.Product.FromSqlRaw("select public.sp_Get_Products()");

所以我仍然无法找到一种方法来通过函数从 postgresql 中获取我的选择结果。

【问题讨论】:

    标签: postgresql .net-core entity-framework-core npgsql sql-function


    【解决方案1】:

    ExecuteSqlCommand 仅用于执行不返回结果的命令。

    FromSqlRaw 用于执行任意 SQL,返回的数据与模型中的实体具有完全相同的形状,因此您有责任确保 sp_Get_Products 返回 Product 中的所有字段。看起来您的函数可能不会返回 Id 列。

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 2022-08-21
      • 2017-10-11
      • 2020-10-26
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多