简体   繁体   English

使用 EF Core 6 在 ASP.NET Core 6 中使用 FromSqlInterpolated 执行存储过程时出错

[英]Error executing stored procedure with FromSqlInterpolated in ASP.NET Core 6 with EF Core 6

I have in my SQL Server this stored procedure with a single parameter and that returns a single record.我在我的 SQL 服务器中有这个带有单个参数并返回单个记录的存储过程。

在此处输入图像描述

When I call it like this:当我这样称呼它时:

public async Task<EmpleadoDTO> GetEmpleado(string codigoSAP)
{
     var empleado = await _context.Empleados
                                  .FromSqlInterpolated($"GetEmpleadoByCodigoSAP {codigoSAP}")
                                  .FirstOrDefaultAsync();

     return EmpleadoToDTO(empleado);
}

I get this error我收到这个错误

System.InvalidOperationException: 'FromSqlRaw' or 'FromSqlInterpolated' was called with non-composable SQL and with a query composing over it. System.InvalidOperationException:“FromSqlRaw”或“FromSqlInterpolated”是使用不可组合的 SQL 调用的,并在其上组合了一个查询。 Consider calling 'AsEnumerable' after the method to perform the composition on the client side.考虑在客户端执行组合的方法之后调用“AsEnumerable”。

But if I execute the SQL directly:但是如果我直接执行SQL:

var empleado = await _context.Empleados
                             .FromSqlInterpolated($"select empID as EmpleadoId,firstName as Nombre,middleName as Apellido1,lastName as Apellido2,Email as Email,U_CODEMPL as CodigoSAP,U_Activo as Activo,U_Sociedad as Sociedad from OHEM where U_CODEMPL={codigoSAP}")
                             .FirstOrDefaultAsync();

it works fine.它工作正常。

Any ideas, please?有什么想法吗?

Thanks谢谢

Starting with EF Core 3.1 FromSqlRaw / FromSqlInterpolated methods when used with stored procedure cannot be composed so you should use AsEnumerable / AsAsyncEnumerable .从 EF Core 3.1 开始, FromSqlRaw / FromSqlInterpolated方法在与存储过程一起使用时无法组合,因此您应该使用AsEnumerable / AsAsyncEnumerable

ref Microsoft docs参考微软文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用EF在asp.net core中使用存储过程 - how to use stored procedure in asp.net core using EF 使用存储过程的 Asp.net 内核中的错误数据是 Null - Error Data is Null in Asp.net core using stored procedure 扩展我的 ASP.NET Core Web API (EF Core) 以调用存储过程 - Extend my ASP.NET Core Web API (EF Core) to call a stored procedure ASP.NET CORE 存储过程错误:InvalidCastException - ASP.NET CORE stored procedure error : InvalidCastException 如何在EF核心和Asp.net核心中调用Stored Proc? - how to call Stored Proc in EF core and Asp.net core? EF Core with Pomelo Adapter 在执行存储过程时返回错误 - EF Core with Pomelo Adapter returns error when executing stored procedure 在 ASP.NET MVC Core(C#) 中执行 Oracle 存储过程 (PL/SQL) - Executing Oracle Stored Procedure (PL/SQL) in ASP.NET MVC Core(C#) EF Core 2使用OUTPUT参数执行存储过程 - EF Core 2 executing Stored Procedure with OUTPUT param EF Core 使用空引用执行存储过程 - EF Core executing stored procedure with null reference 如何在 Dot Net Core 3.1 中使用 FromSqlInterpolated/Database.ExecuteSqlInterpolated 执行带输出参数的存储过程? - How to execute stored procedure with Output parameters using FromSqlInterpolated/Database.ExecuteSqlInterpolated in Dot Net Core 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM