是否可以使用EF Core 3 FromSqlRaw创建新的对象列表

时间:2019-10-17 05:02:36

标签: linq ef-core-3.0

我正在使用linq查询,该查询使用带有FromSqlRaw的DbSet。 我正在尝试获取其他对象列表。

var habitaciones = await _context.HabitacionDisponibilidad 
    .FromSqlRaw("pg_get_habitacionDisponibles {0},{1},{2},{3},{4},{5}", 
     edificio.EdificioId, sm.Huespedes, sm.FechaInicial, sm.FechaFinal, EstatusDisponibilidad.Disponible, cantidadNoches)
     .Select(p => new HabitacionDisponible
     {
            EdificioId = p.EdificioId,
            HabitacionDisponibilidadId = p.HabitacionDisponibilidadId,
            HabitacionId = p.HabitacionId,
            TipoHabitacionId = p.Habitacion.TipoHabitacionId,
            Tarifa = p.Tarifa
     })
     .ToListAsync();

如果我删除此代码,查询将起作用

     .Select(p => new HabitacionDisponible
     {
            EdificioId = p.EdificioId,
            HabitacionDisponibilidadId = p.HabitacionDisponibilidadId,
            HabitacionId = p.HabitacionId,
            TipoHabitacionId = p.Habitacion.TipoHabitacionId,
            Tarifa = p.Tarifa
     })

这是在sql服务器中运行的sql coe:

exec sp_executesql N'SELECT [h].[EdificioId], [h].[HabitacionDisponibilidadId], [h].[HabitacionId], [h0].[TipoHabitacionId], [h].[Tarifa]
FROM
(
   pg_get_habitacionDisponibles @p0, @p1, @p2, @p3, @p4, @p5
) AS [h]
INNER JOIN [Habitacion] AS [h0] ON [h].[HabitacionId] = [h0].[HabitacionId]',N'@p0 uniqueidentifier,@p1 int,@p2 datetime2(7),@p3 datetime2(7),@p4 int,@p5 float',@p0='AD8FEBD4-AF89-4EDE-5300-08D72BF290CE',@p1=1,@p2='2019-10-15 00:00:00',@p3='2019-10-18 00:00:00',@p4=1,@p5=4

这是我在运行查询时遇到的错误:

Microsoft.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@p0'.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext 
executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
ClientConnectionId:ab3f2f48-7ec6-4518-9be5-cc9ae15bbefb
Error Number:102,State:1,Class:15

这是实现我想要实现的目标的正确方法,还是您可以给我其他建议?

0 个答案:

没有答案