我正在使用最新的 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/