【发布时间】:2021-09-13 06:49:48
【问题描述】:
我正在使用最新的 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 解决的问题,但显然不是。
【问题讨论】:
-
用
select * from (exec ...)包装存储过程调用 -
修复了不同的问题。无法在 SqlServer 中编写 SP(尝试在 SQL 中编写,您会看到)。用 TVF 替换 SP 是唯一的选择。
-
select * from (exec ...) 在 SSMS 中的语法不正确,并在 C# 中抛出“语法错误”的“SqlException”
-
在 SP
SELECT TOP 1 * FROM (exec ProcedureName)上作曲的示例。这不是有效的 SQL 查询。所以你需要打电话给FromSqlRaw(...).AsEnumerable() -
谢谢@FireAlkazar,但这不是我正在做的事情吗?或者可能是因为我使用 `_context.Entity` 来运行这个 SP,它会自动用“SELECT”包围它?