【问题标题】:EF Core stored procedure with 2 parameters error System.InvalidOperationException:带有 2 个参数错误 System.InvalidOperationException 的 EF Core 存储过程:
【发布时间】:2020-11-23 01:22:12
【问题描述】:

我正在迁移到 EF Core 并尝试使用 2 个参数调用存储过程。

1 个参数的当前设置工作正常。

这是我看到的错误:

System.InvalidOperationException:FromSqlRaw 或 FromSqlInterpolated 是使用不可堆肥的 SQL 调用的,并且在其上进行了查询。考虑在 FromSqlRaw 或 FromSqlInterpolated 方法之后调用 AsEnumerable 以在客户端执行组合。在

这是我的存储库调用:

public IQueryable<PartsGridDTO> GetPartsGridQueryable(int partId, int groupId)
{
    return MoreContext.SpPartsGridDetailYourCases
                      .FromSqlRaw($"EXECUTE dbo.GetProductsByPartAndGroup {partId}, {groupId}")
                      .Select(s => new PartsGridDTO
                                   {
                                       PartId = partId,
                                       //....
                                   }).AsQueryable();
}

我从这里尝试了一些修复:

Include with FromSqlRaw and stored procedure in EF Core 3.1

但这些都不起作用。

我将返回AsQueryable() 直到控制器,然后调用.ToListAsync()

我尝试在.AsQueryable() 之前添加.IgnoreQueryFilters();,但我仍然看到错误。

有什么想法吗?我是否传递了错误的参数?是 EF Core 问题还是?

【问题讨论】:

  • @JuanPablo:我试过这些,但它仍然给我同样的错误:-( .FromSqlRaw($"GetProductsByPartAndGroup @p0, @p1", partId, groupId)
  • @JuanPablo 和 .FromSqlRaw($"GetProductsByPartAndGroup @p0 @p1", partId, groupId)

标签: entity-framework-core


【解决方案1】:

在我解决问题之前有 3 个项目:

  1. 我有额外的属性不在存储过程的返回范围内。我猜这会导致我在上面发布的错误。

  2. (一个子问题)我也只需要返回网格要显示的项目。这意味着网格可能仅显示存储过程中返回的 8 个值中的 5 个。在调用的存储过程的返回项中有这些额外的列导致了这个错误:InvalidOperationException: The required column 'Address' was not present in the results of a 'FromSql' operation

  3. 这是我存储的过程代码的最终外观:

         return Context.EntityExample.FromSqlRaw($"dbo.GetProductsByPartAndGroup {partId}, {groupId}")

【讨论】:

  • 感谢@JuanPablo 提供链接!
猜你喜欢
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多