报错Field not found in row: sugarIndex 返回

SqlSugar 沟通中
2 290
该叫什么 4.20_ 发布于2025/7/31
悬赏:0 飞吻
【异常信息】:"Field not found in row: sugarIndex" 
【异常类型】:"IndexOutOfRangeException" 
【堆栈调用】:"   at Npgsql.BackendMessages.RowDescriptionMessage.GetFieldIndex(String name)
   at SqlSugar.DbBindAccessory.SetAppendColumns(IDataReader dataReader)
   at SqlSugar.DbBindAccessory.GetEntityListAsync[T](SqlSugarProvider context, IDataReader dataReader)
   at SqlSugar.AdoProvider.GetDataAsync[TResult](Type entityType, IDataReader dataReader)
   at SqlSugar.AdoProvider.SqlQueryAsync[T,T2,T3,T4,T5,T6,T7](String sql, Object parameters)
   at SqlSugar.AdoProvider.SqlQueryAsync[T](String sql, SugarParameter[] parameters)
   at AdmBoots.Domain.BasePtypes.BasePtypeRepository.GetCarKTypePtypeAvailableStockQty(List`1 ptypeID, List`1 ktypeID, Int32 billID, Int32 dstbBillID, Int32 settBillID) in D:\Git\CoreDemo\AdmBoots.Domain\BasePtypes\BasePtypeRepository.cs:line 1845
   at AdmBoots.Application.InvGoodsStockJobss.InvGoodsStockJobsService.GetInvGoodsStockJobsList(GetInvGoodsStockJobsListInput input) in D:\Git\CoreDemo\AdmBoots.Application\InvGoodsStockJobss\InvGoodsStockJobsService.cs:line 93
   at AdmBoots.Api.Controllers.InvGoodsStockJobsController.GetInvGoodsStockJobsList(GetInvGoodsStockJobsListInput input) in D:\Git\CoreDemo\AdmBoots.Api\Controllers\InvGoodsStockJobsController.cs:line 46
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   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>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)"
上面是堆栈信息,下面是GetCarKTypePtypeAvailableStockQty的代码

 

public async Task<List<GetBasePtypeAvailableStockQty>> GetCarKTypePtypeAvailableStockQty(List<int> ptypeID, List<int> ktypeID, int billID = 0, int dstbBillID = 0, int settBillID = 0) {
     var sql = string.Format(@"SELECT a.PTypeID, SUM(a.qty) AS AvailableStockQty,a.GoodsStockJobID FROM(
         SELECT c.PTypeID,sum(d.loadedqty * d.unitrate) AS qty ,max(e.id) as GoodsStockJobID
         FROM bill_distributionindex A 
         INNER JOIN bill_distribution_detail B ON A.id = B.DistributionID
         LEFT JOIN bill_saledetail C ON b.DetailID = c.id
         left join bill_salestockjobs d on c.bindid = d.bindid and c.billid = d.billid
         LEFT JOIN inv_goodsstockjobs e on d.ptypeid = e.ptypeid and e.ktypeid = a.DeliveryKTypeID and d.outfactorydate = e.outfactorydate
         WHERE  a.id <> {4} AND b. BillID <>{3} and  C.SaleType<>1 AND a. LoadingStatus=1 AND a.DeliveryStatus<>1 AND a.DeliveryKTypeID in ({0})
         AND c.PTypeID IN ({1})
         group by c.ptypeid,e.id
         UNION ALL
         SELECT
           d.PTypeID,
           d.qty AS qty,
           d.GoodsStockJobID as GoodsStockJobID
         FROM
           bill_distributionindex A
           INNER JOIN bill_distribution_billinfo B ON A.id = B.DistributionID AND b.ReturnAllotBillID>0
           LEFT JOIN bill_orderindex C ON b.ReturnAllotBillID = c.id
           LEFT JOIN bill_allotdetail D ON c.id = D.billid
         WHERE
           a.DeliveryKTypeID in ({0})
           AND d.PTypeID IN ({1})
           AND c.AuditStatus IN (1,2)
         UNION ALL
         SELECT
           d.PTypeID,
           d.qty AS qty,
           d.GoodsStockJobID as GoodsStockJobID
         FROM
           bill_distributionindex A
           INNER JOIN bill_distribution_billinfo B ON A.id = B.DistributionID AND b.refusalallotbillid>0
           LEFT JOIN bill_orderindex C ON b.refusalallotbillid = c.id
           LEFT JOIN bill_allotdetail D ON c.id = D.billid
         WHERE
           a.DeliveryKTypeID in ({0})
           AND d.PTypeID IN ({1})
           AND c.AuditStatus IN (1,2)
         UNION ALL
         SELECT
           b.PTypeID,
           b.qty AS qty,
           b.GoodsStockJobID as GoodsStockJobID
         FROM
           bill_orderindex A
           INNER JOIN bill_settlementdetail B ON a.id = b.billid
         WHERE
           a.ktypeid in ({0})
           AND b.PTypeID IN ({1})
           and a.DistributionID = 0 and a.IsDraft = 0 and a.RedFlag = 0 and a.IsVoid = 0 and b.SaleType <> 1
           AND a.AuditStatus IN (1,2) {2}
         ) a GROUP BY A.ptypeid,a.GoodsStockJobID", string.Join(",", ktypeID), string.Join(",", ptypeID), settBillID > 0 ? $" and a.id <> {settBillID}" : "", billID, dstbBillID);
     return await _sqlClient.Ado.SqlQueryAsync<GetBasePtypeAvailableStockQty>(sql);
 }

下面是初始化_sqlClient的代码,结构是sass分库,多租户模式

        public ISqlSugarClient Getclint(int companyId = 0) {
            var id = (companyId == 0 ? _admSession?.CompanyId : companyId) ?? 0;
            return id != 0 ? GetScopeClient(id) : default;
        }
        private ISqlSugarClient GetScopeClient(int id) {
            if (_online.IsAnyConnection(id))
                return _online.GetConnectionScope(id);
            // 获取连接字符串
            var dbstring = _cache.GetDBstring(id);
            // 如果连接字符串为空直接返回
            if (string.IsNullOrEmpty(dbstring)) return default;
            _online.AddConnection(new ConnectionConfig {
                ConfigId = id,
                ConnectionString = dbstring,
                DbType = DbType.PostgreSQL,//数据库类型
                IsAutoCloseConnection = true,
                ConfigureExternalServices = new ConfigureExternalServices() {
                    SqlFuncServices = SqlFuncExtensions.FuncExternal()
                }
            });
            var res = _online.GetConnectionScope(id) ?? default;
#if DEBUG
            res.Aop.OnLogExecuting = (sql, pars) => {
                Console.WriteLine();
                Console.WriteLine(UtilMethods.GetSqlString(DbType.PostgreSQL, sql, pars));//输出sql
                //Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));//参数
            };
#endif
            return res;
        }

使用sql才会出现这种情况,使用sqlsuagr的语法不会出现这个问题,加copyNew()可以解决,但是我们在外层用了封装了事务,使用copyNew会导致无法读取未提交的数据

热忱回答2