简体   繁体   中英

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.

在此处输入图像描述

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. Consider calling 'AsEnumerable' after the method to perform the composition on the client side.

But if I execute the SQL directly:

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 .

ref Microsoft docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM