SqlSugar常见问题汇总

1、已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。

There is already an open DataReader associated with this Connection which must be closed first.

或者出现 connection is closed

出现这个错一般是线程安全引起的

解决方案: https://www.donet5.com/Home/Doc?typeId=1224

 

2.The connection does not support MultipleActiveResultSets

 (1)、SqlSugarClient 替换成 SqlSugarScope ,因为SqlSugarScope 是线程安全对象代码容错率高

   具体用法: https://www.donet5.com/Home/Doc?typeId=1181

   (2)  、异步用法错引起的 ,排查没用Await调用的异步方法

Public void  GetAll(){  异步方法  }//错误
Public async Task GetAll(){  异步方法  }//错误
Public async Task GetAll(){  await  异步方法  }//正确

 

 3. max_allowed_packet  MYSQL

一般提示这个错可能一次更新的内容太多,可以分页操作

db.Utilities.PageEach(allList, 10000 ,pageList=> {
  db.Insertable(pageList).ExecuteCommand();
});

 

4、实体与表映射出错

出现这个错一般是 数据库中的类型和实体中的类型不一样引起的,可以注释实体类中的字段进行排除哪个字段引起的

 

5、Only one primary key

当前功能只支持单主键,看看实体类有没有与置主键,并且主键只能有一个,或者数据库是否有主键并且只能有一个

 

6、未安装或者版本冲突

打开 Web层或者WinFom的项目,查看 App.config 有没有下面这种兼容配置,如果没这种配置这个DLL就会报错,

想办法搞出来,一般NUGET在当前项目安装 SqlSugar然后在安装报错的DLL NUGET会自动添加到app.config

例1:

   
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>

例2:

  <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.27.0" newVersion="8.0.27.0" />
   </dependentAssembly>

如果没有先安装SqlSugar,在安装报错dll

 

7、雪花ID重复

 不同电脑 本地或者服务器 要用不同的WorkId ,如果服务器时间回调也要设置WorkId

 

8、Cannot Open when State is Connecting.

解决方案: https://www.donet5.com/Home/Doc?typeId=1224

 

9、连接不上数据库

原生能用SqlSugar就能用,一定要用db.CurrentConfig.ConnectionString保证是同一个字段串,并且在同一个程序

//用原代码进行测试
//SqlSerer 
var conn=new SqlConnection(db.CurrentConfig.ConnectionString).Open();
conn.Close();
 
//MySql用
var conn=new MySqlConnection(db.CurrentConfig.ConnectionString).Open();
conn.Close();
 
//不同的数据库不同 ,一般根据DbType+Connection

 

10、截断二进制

Data too long for column

数据库字段太小引起的

 

11、特殊表字段名带有.或者()这字符兼容

因为考虑性能,比如数据库里面字段名字叫: 金额(元) ,生在SQL如下 where [金额(元)]=@金额(元) ,字段能转译参数名做不到所有都过滤,

 这样参名字会报错,所以ORM提供了AOP来处理这个问题

  db.Aop.OnExecutingChangeSql = (s, p) =>
            {
                foreach (var item in p)
                {
                    if (item.ParameterName.Contains("("))
                    {
                        var oldName = item.ParameterName;
                        //重点看这儿,把参数名变成正常的名字
                        item.ParameterName = item.ParameterName
                                    .Replace("(""_")
                                    .Replace(")""_")
                                    .Replace(".""_");
                        s = s.Replace(oldName, item.ParameterName);
                    };
                }
                return new KeyValuePair<string, SugarParameter[]>(s, p);
            };

 

12、与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务。

(1) 出现这个错一般是超过数据库处理上限,可以用异步提升性能,max pool size字符串设置大些

(2) 并发不高情况一般是线程安全引起的, 看文档:偶发错误

 

13、The connection was not closed. The connection's current state is connecting

看文档:偶发错误

 

14、参数名有空格特殊符号

对性能会有影响,能局部修改配置就局部,尽量不要全局使用

//全局生效
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
    ConnectionString = $"Data Source={dbFileName};",
    DbType = DbType.Sqlite,
    IsAutoCloseConnection = true,
    MoreSettings = new ConnMoreSettings
    {
        IsCorrectErrorSqlParameterName= true// 5.1.4.111及以上版本
    }
});
 
//局部生效
db.CurrentConnectionConfig.MoreSettings=new MoreSettings(){  IsCorrectErrorSqlParameterName= true}

 

15、Connect Timeout expired. All pooled connections are in use

并发不高出现这个错一般是线程安全问题

posted @ 2024-01-16 15:48  yswenli  阅读(2251)  评论(0编辑  收藏  举报