[英]ASP.NET CORE stored procedure error : InvalidCastException
Good day, I have this store procedure because of the recursion of the query if I will put this on to LINQ style of code or procedural, it will may cause performance issue along the way.美好的一天,由于查询的递归,我有这个存储过程,如果我将它放在 LINQ 样式的代码或程序上,它可能会导致性能问题。
so I have this store procedure所以我有这个存储过程
CREATE PROCEDURE GetReferralTree @parentID nvarchar(450)
AS
WITH referral_CTE (Id, Fullname, Referral)AS
(
SELECT dbo.AspNetUsers.Id,dbo.AspNetUsers.Fullname, dbo.AspNetUsers.Referral from AspNetUsers where Id = @parentID
union all
SELECT AspNetUsers.Id, AspNetUsers.Fullname, AspNetUsers.Referral
from AspNetUsers
JOIN referral_CTE ON AspNetUsers.Referral = referral_CTE.id
)
SELECT distinct * FROM referral_CTE
GO;
and I called it on my asp.net core web app我在我的 asp.net 核心 web 应用程序上调用它
[HttpGet]
public ActionResult GetReferralTree(string id)
{
List<ReferralTreeViewModel> hierarchy = new List<ReferralTreeViewModel>();
var parentIDParam = new SqlParameter("@parentID", id);
hierarchy = (List<ReferralTreeViewModel>)_context.AspNetUsers.FromSqlRaw("exec GetReferralTree @parentID ", parentIDParam)
.Select(x => new ReferralTreeViewModel()
{
Id = x.Id,
Fullname = x.Fullname,
ParentId = x.Referral
})
.AsEnumerable() ;
return Ok(hierarchy);
}
but returns something like this但返回这样的东西
System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Genea.Areas.Admin.Models.ReferralTreeViewModel]' to type 'System.Collections.Generic.List`1[Genea.Areas.Admin.Models.ReferralTreeViewModel]'.
at Genea.Areas.Admin.Controllers.GenealogyController.GetReferralTree(String id) in C:\Users\winshiero\source\repos\Genea\Areas\Admin\Controllers\GenealogyController.cs:line 43
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at AspNetCoreHero.ToastNotification.Middlewares.NotyfMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass5_1.<<UseMiddlewareInterface>b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Try to use this:尝试使用这个:
[HttpGet]
public ActionResult<List<ReferralTreeViewModel>> GetReferralTree(string id)
{
var parentIDParam = new SqlParameter("@parentID", id);
var hierarchy = _context.AspNetUsers.FromSqlRaw("exec GetReferralTree @parentID ",
parentIDParam).ToArray();
var result hierarchy.Select(x => new ReferralTreeViewModel
{
Id = x.Id,
Fullname = x.Fullname,
ParentId = x.Referral
}).ToList();
return Ok(result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.