果糖网

加了Filter过滤后,报异常 返回

SqlSugar
3 90
public static IServiceCollection AddSqlSugarBatchExt(this IServiceCollection services, List<DbOptions> settings)
        {
            var defConn = settings.Where(s => s.DbName == "CS").First();
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConfigId = defConn.DbName,
                DbType = (DbType)defConn.DbType,
                ConnectionString = defConn.Coon,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });
            db.QueryFilter.Add(new TableFilterItem<TableName>(it => it.Name== "1003"));
            settings.Remove(defConn);
            foreach (var item in settings)
            {
                var config = new ConnectionConfig()
                {
                    ConfigId = item.DbName,
                    DbType = (DbType)item.DbType,
                    ConnectionString = item.Coon,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                };
                db.AddConnection(config);
            }

            return services.AddScoped<SqlSugarClient>(provider =>
            {
                return db;
            });


        }

后面报错是在各个查询页面报的

1.       System.InvalidOperationException:Invalid attempt to call MetaData when reader is closed.

2.       System.InvalidOperationException:ExecuteReader requires an open and available Connection. The connection's current state is open.

3.       SqlSugar.SqlSugarException:English Message : Connection open error . There is already an open DataReader associated with this Connection which must be closed first.

Chinese Message :  连接数据库过程中发生错误检查服务器是否正常连接字符串是否正确实在找不到原因请先Google错误信息:There is already an open DataReader associated with this Connection which must be closed first..

4. SqlSugar.SqlSugarException:English Message : Connection open error . The connection was not closed. The connection's current state is open.

Chinese Message :  连接数据库过程中发生错误检查服务器是否正常连接字符串是否正确实在找不到原因请先Google错误信息:The connection was not closed. The connection's current state is open..

5.System.InvalidOperationException:Invalid attempt to call FieldCount when reader is closed.

6.System.InvalidOperationException:Invalid operation. The connection is closed.

7. SqlSugar.SqlSugarException:English Message : Entity mapping error.Invalid attempt to call Read when reader is closed.

Chinese Message : 实体与表映射出错。Invalid attempt to call Read when reader is closed.

8. System.InvalidOperationException:There is already an open DataReader associated with this Command which must be closed first.

9. SqlSugar.SqlSugarException:“English Message : Connection open error . Unable to cast object of type 'System.Data.ProviderBase.DbConnectionClosedConnecting' to type 'System.Data.SqlClient.SqlInternalConnectionTds'.

Chinese Message :  连接数据库过程中发生错误,检查服务器是否正常连接字符串是否正确,实在找不到原因请先Google错误信息:Unable to cast object of type 'System.Data.ProviderBase.DbConnectionClosedConnecting' to type 'System.Data.SqlClient.SqlInternalConnectionTds'..”


尝试过连接单库,1-4都是单库报的异常


另外在数据库连接串不加MultipleActiveResultSets=true;的时候报异常8



热忱回答3

  • 出现这个错一般是线程安全没注意,静止单例,异步是否正确使用,是否有中间件导致的

    0 回复
  •  return services.AddScoped<SqlSugarClient>(provider =>
                {
                    return new db;
                });

    这样才是对的 不是直接用db

    0 回复
  • @fate stay night:好的,感谢指导

    0 回复

学习文档