0

I have in my SQL Server this stored procedure with a single parameter and that returns a single record.

enter image description here

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

Progman
  • 16,827
  • 6
  • 33
  • 48
kintela
  • 1,283
  • 1
  • 14
  • 32
  • 2
    According to your SP, it seems you have three parameter, but your sql command is just pass two parameter. Please change the it like below and try again: $"GetActividadesByEmpleadoMesAño {usuario.internal_k}, {year}, {mes} – Brando Zhang Oct 31 '22 at 03:06
  • Hi In this case, when returning a list, everything works correctly, but if I execute an sp that returns a single element, 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. – kintela Nov 07 '22 at 08:07

1 Answers1

0

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

Kaloyan Drenski
  • 950
  • 1
  • 13
  • 18