更新错误ArgumentOutOfRangeException这个问题出现了好多次 返回

求助
2 63
该叫什么 Rookie 发布于1周前
悬赏:20 飞吻

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'

我想根据id更新里面的5个字段,可就是报错。

方法定义:ModifySingleWithBatch(StoreDto store, string modifyUserId)


报错代码如下:

int rows = Db.Updateable<StoreEntity>()
.SetColumns(s => new StoreEntity()
{
    ModifyUserId = Guid.Parse(modifyUserId),
    ModifyTime = DateTime.Now,
    RUserId = Guid.Parse(store.RUserId),
    RTime = store.RTime,
    RoomNo = store.RoomNo
})
.Where(s => s.Id == Guid.Parse(store.Id))
.ExecuteCommand();


已打过断点,所有传入的值都是正常的。


完整控制台异常如下:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at SqlSugar.DefaultDbMethod.Equals(MethodCallExpressionModel model)
   at SqlSugar.MethodCallExpressionResolve.GetMethodValue(String name, MethodCallExpressionModel model)
   at SqlSugar.MethodCallExpressionResolve.Where(ExpressionParameter parameter, Nullable`1 isLeft, String name, IEnumerable`1 args, MethodCallExpressionModel model, List`1 appendArgs)
   at SqlSugar.MethodCallExpressionResolve.SqlFuncMethod(ExpressionParameter parameter, MethodCallExpression express, Nullable`1 isLeft)
   at SqlSugar.MethodCallExpressionResolve..ctor(ExpressionParameter parameter)
   at SqlSugar.BaseResolve.Start()
   at SqlSugar.BinaryExpressionResolve..ctor(ExpressionParameter parameter)
   at SqlSugar.BaseResolve.Start()
   at SqlSugar.LambdaExpressionResolve..ctor(ExpressionParameter parameter)
   at SqlSugar.BaseResolve.Start()
   at SqlSugar.ExpressionContext.Resolve(Expression expression, ResolveExpressType resolveType)
   at SqlSugar.UpdateBuilder.GetExpressionValue(Expression expression, ResolveExpressType resolveType, Boolean isMapping)
   at SqlSugar.UpdateableProvider`1.Where(Expression`1 expression)
   at backend.Api.Db.BLL.StoreBLL.ModifySingleWithBatch(StoreDto store, String modifyUserId) in J:\phase3\backend\backend.Api\Db\BLL\StoreBLL.cs:line 202
   at backend.Api.Controller.StoresController.BatchModifyStore(List`1 storeDtoList, String userId) in J:\phase3\backend\backend.Api\Controller\StoresController.cs:line 119
   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.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   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 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

热忱回答2

  • 还有其他情况:有时候用simpleClient里定义的XXXDb可以成功CRUD,但换成sqlSugarClient就不能了,不知道是我的问题还是其他问题,很迷。

    像这种范围越界异常就让人很迷茫,错在哪都不知道。

    0 回复
  • 我找到问题所在了,我的Dto中有Entity没有的字段。我猜测,他会把所有dto的字段去entity中找,找不到就抛出错误。

    我的解决办法,在controller里把dto先转成entity,只对entity传参。对象映射我采用的autoMapper,改完后一切正常了。

    0 回复

版块

学习文档

最新会员

发布达人

回贴达人