EF Core 3.1:使用 FromSqlRaw 查询存储过程时序列不包含任何元素

OPu*_*idt 5 c# entity-framework entity-framework-core

我尝试使用 Entity Framework Core 3.1 查询存储过程。我做了一个非常简单的测试项目。

存储过程:

ALTER PROCEDURE [dbo].[prcTest]
AS
BEGIN

    SET NOCOUNT ON;

    select tbl_Postalcode.PST_T_PostalCode AS Postalcode
    from tbl_Postalcode
END
Run Code Online (Sandbox Code Playgroud)

实体:

public class Test
{
    string Postalcode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我使用这个扩展方法,在 Stackoverflow 上找到,它稍微封装了整个事情。但没什么特别的:

ALTER PROCEDURE [dbo].[prcTest]
AS
BEGIN

    SET NOCOUNT ON;

    select tbl_Postalcode.PST_T_PostalCode AS Postalcode
    from tbl_Postalcode
END
Run Code Online (Sandbox Code Playgroud)

然后我像这样执行查询:

var result = await _myContext.SqlQueryAsync<Test>("Exec prcTest");
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

AggregateException: One or more errors occurred. (Sequence contains no elements)
System.Threading.Tasks.Task.ThrowIfExceptional(bool includeTaskCanceledExceptions)

InvalidOperationException: Sequence contains no elements
System.Linq.ThrowHelper.ThrowNoElementsException()


System.Threading.Tasks.Task.ThrowIfExceptional(bool includeTaskCanceledExceptions)
System.Threading.Tasks.Task<TResult>.GetResultCore(bool waitCompletionNotification)
System.Threading.Tasks.Task<TResult>.get_Result()
System.Text.Json.JsonPropertyInfoCommon<TClass, TDeclaredProperty, TRuntimeProperty, TConverter>.GetValueAsObject(object obj)
System.Text.Json.JsonSerializer.HandleEnumerable(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, ref WriteStack state)
System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, int originalWriterDepth, int flushThreshold, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我具体问题是什么?

OPu*_*idt 10

找到了解决方案。该错误消息没有帮助并且非常令人困惑。

解决方案:

将财产公开。这是很明显的,但错误消息让我困惑。

public class Test
{
    public string Postalcode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!以防万一其他人遇到这种情况,在我的例子中,属性是公开的,但他们没有 { get; 放; 我仍然遇到这个非常非描述性的错误。 (4认同)