c# - 使用带有 EFCore5.0 错误 System.InvalidOperationException 的存储过程

标签 c# entity-framework-core

我正在使用最新的 EF.core 版本 (5.0.7) 并尝试通过存储过程检索实体。 尝试了以下操作:

 //attemp 1
 res = _context.Entity.FromSqlRaw($"exec dbo.ProcedureName");
 //attemp 2
 res = _context.Entity.FromSqlRaw("exec ProcedureName", new SqlParameter("@ParamName", paramValue));
 //attemp 3
 res = _context.Entity.FromSqlRaw($"exec ProcedureName {paramValue}");
 //attemp 4
 res = _context.Entity.FromSqlInterpolated($"exec ProcedureName @ParamName = {paramValue}");
 //attemp 5
 res = _context.Entity.FromSqlInterpolated($"exec ProcedureName {paramValue}");

所有示例,不带“exec”,带和不带“@”的参数名称以及带和不带前导“dbo”的过程名称 我不断收到错误消息“System.InvalidOperationException:FromSqlRaw 或 FromSqlInterpolated 是使用不可堆肥的 SQL 调用的,并在其上组合了一个查询。考虑在 FromSqlRaw 或 FromSqlInterpolated 方法之后调用 AsEnumerable 以在客户端执行组合。” 我做错了什么?

附言

客户端组合不是一个选项。

该过程在 SSMS 中运行良好。

another thread据称这是由 EF5.0 解决的问题,但显然不是。

最佳答案

为了使用FromSqlRaw,您需要让进行插值,它会将其转换为参数。 不要自己做。

res = _context.Entity
  .FromSqlRaw("exec ProcedureName @ParamName = {paramValue}", paramvalue)
  .AsEnumerable();

注意缺少$。您还需要添加 AsEnumerable to prevent it from trying to compose it .

关于c# - 使用带有 EFCore5.0 错误 System.InvalidOperationException 的存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68206153/

相关文章:

c# - N-S 和 E-W 格式的纬度/经度正则表达式

postgresql - 使用 NetTopologySuite 与 EF Core 和 postgis 计算两点之间的地理距离

c# - Entity Framework 核心 3.1 与 NetTopologySuite.Geometries.Point : SqlException: The supplied value is not a valid instance of data type geography

c# - 从 IDesignTimeDbContextFactory<T> 访问 App.config

entity-framework - Entity Framework Core,无法在主键上设置主机名

c# - Entity Framework Core 不会映射到 BigInteger

c# - 不同的列表 LINQ 查询

c# - IEnumerable<XElement> 和 foreach

c# - 保持演示者引用处于事件状态,而不提供对其控制的 View 的引用

c# - Unity - 无法访问对另一个脚本的引用