翼度科技»论坛 编程开发 .net 查看内容

SqlSugar入门

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
{","marks":[]}]}]},{"type":"block","id":"5le2-1705386952815","name":"code-line","data":{},"nodes":[{"type":"text","id":"HtoI-1705386952814","leaves":[{"text":"                 //5.1.3.24统一了语法和SqlSugarScope一样,老版本AOP可以写外面","marks":[]}]}]},{"type":"block","id":"8jkJ-1705386952817","name":"code-line","data":{},"nodes":[{"type":"text","id":"83LC-1705386952816","leaves":[{"text":"                 ","marks":[]}]}]},{"type":"block","id":"CEHr-1705386952819","name":"code-line","data":{},"nodes":[{"type":"text","id":"Azkl-1705386952818","leaves":[{"text":"                  db.Aop.OnLogExecuting = (sql, pars) =>","marks":[]}]}]},{"type":"block","id":"Hd9p-1705386952821","name":"code-line","data":{},"nodes":[{"type":"text","id":"sUgt-1705386952820","leaves":[{"text":"                  {","marks":[]}]}]},{"type":"block","id":"u1fh-1705386952823","name":"code-line","data":{},"nodes":[{"type":"text","id":"qFgX-1705386952822","leaves":[{"text":"                    Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响","marks":[]}]}]},{"type":"block","id":"Smjz-1705386952825","name":"code-line","data":{},"nodes":[{"type":"text","id":"8oHT-1705386952824","leaves":[{"text":"                    ","marks":[]}]}]},{"type":"block","id":"4FaS-1705386952827","name":"code-line","data":{},"nodes":[{"type":"text","id":"8wYb-1705386952826","leaves":[{"text":"                    ","marks":[]}]}]},{"type":"block","id":"KIfd-1705386952829","name":"code-line","data":{},"nodes":[{"type":"text","id":"TAWP-1705386952828","leaves":[{"text":"                    //获取原生SQL推荐 5.1.4.63  性能OK","marks":[]}]}]},{"type":"block","id":"wNTa-1705386952831","name":"code-line","data":{},"nodes":[{"type":"text","id":"a4fP-1705386952830","leaves":[{"text":"                     Console.WriteLine(UtilMethods.GetNativeSql(sql,pars))","marks":[]}]}]},{"type":"block","id":"wS7i-1705386952833","name":"code-line","data":{},"nodes":[{"type":"text","id":"essT-1705386952832","leaves":[{"text":"                               ","marks":[]}]}]},{"type":"block","id":"QghF-1705386952835","name":"code-line","data":{},"nodes":[{"type":"text","id":"PedH-1705386952834","leaves":[{"text":"                    //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用","marks":[]}]}]},{"type":"block","id":"wbMg-1705386952837","name":"code-line","data":{},"nodes":[{"type":"text","id":"HS8P-1705386952836","leaves":[{"text":"                    //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)","marks":[]}]}]},{"type":"block","id":"a7NK-1705386952839","name":"code-line","data":{},"nodes":[{"type":"text","id":"szV1-1705386952838","leaves":[{"text":"                     ","marks":[]}]}]},{"type":"block","id":"dn0H-1705386952841","name":"code-line","data":{},"nodes":[{"type":"text","id":"3ymU-1705386952840","leaves":[{"text":"                    ","marks":[]}]}]},{"type":"block","id":"UzEK-1705386952843","name":"code-line","data":{},"nodes":[{"type":"text","id":"05Lf-1705386952842","leaves":[{"text":"                   };","marks":[]}]}]},{"type":"block","id":"PvDJ-1705386952845","name":"code-line","data":{},"nodes":[{"type":"text","id":"6qEJ-1705386952844","leaves":[{"text":"                   ","marks":[]}]}]},{"type":"block","id":"vmRd-1705386952847","name":"code-line","data":{},"nodes":[{"type":"text","id":"LuvO-1705386952846","leaves":[{"text":"                   //注意多租户 有几个设置几个","marks":[]}]}]},{"type":"block","id":"ZZyH-1705386952849","name":"code-line","data":{},"nodes":[{"type":"text","id":"lEel-1705386952848","leaves":[{"text":"                   //db.GetConnection(i).Aop","marks":[]}]}]},{"type":"block","id":"Gtnx-1705386952851","name":"code-line","data":{},"nodes":[{"type":"text","id":"oI0F-1705386952850","leaves":[{"text":"           ","marks":[]}]}]},{"type":"block","id":"FoCL-1705386952853","name":"code-line","data":{},"nodes":[{"type":"text","id":"Ctme-1705386952852","leaves":[{"text":"           });","marks":[]}]}]},{"type":"block","id":"EXb9-1705386952855","name":"code-line","data":{},"nodes":[{"type":"text","id":"TOZ8-1705386952854","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"fwpQ-1705386952857","name":"code-line","data":{},"nodes":[{"type":"text","id":"nB4U-1705386952856","leaves":[{"text":"     //建表","marks":[]}]}]},{"type":"block","id":"fV3x-1705386952859","name":"code-line","data":{},"nodes":[{"type":"text","id":"8WM3-1705386952858","leaves":[{"text":"     //db.CodeFirst.InitTables(); 更多看文档迁移        ","marks":[]}]}]},{"type":"block","id":"gpmI-1705386952861","name":"code-line","data":{},"nodes":[{"type":"text","id":"leel-1705386952860","leaves":[{"text":"           ","marks":[]}]}]},{"type":"block","id":"Sc4j-1705386952863","name":"code-line","data":{},"nodes":[{"type":"text","id":"jAde-1705386952862","leaves":[{"text":"     //查询表的所有","marks":[]}]}]},{"type":"block","id":"NAeb-1705386952865","name":"code-line","data":{},"nodes":[{"type":"text","id":"WDvp-1705386952864","leaves":[{"text":"     var list = db.Queryable().ToList();","marks":[]}]}]},{"type":"block","id":"SR2s-1705386952867","name":"code-line","data":{},"nodes":[{"type":"text","id":"tjoK-1705386952866","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"gW54-1705386952869","name":"code-line","data":{},"nodes":[{"type":"text","id":"ITtx-1705386952868","leaves":[{"text":"     //插入","marks":[]}]}]},{"type":"block","id":"p3te-1705386952871","name":"code-line","data":{},"nodes":[{"type":"text","id":"wnWe-1705386952870","leaves":[{"text":"     db.Insertable(new Student() { SchoolId = 1, Name = \"jack\" }).ExecuteCommand();","marks":[]}]}]},{"type":"block","id":"SoLB-1705386952873","name":"code-line","data":{},"nodes":[{"type":"text","id":"V2oH-1705386952872","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"mocN-1705386952875","name":"code-line","data":{},"nodes":[{"type":"text","id":"Oe3D-1705386952874","leaves":[{"text":"     //更新","marks":[]}]}]},{"type":"block","id":"RIXw-1705386952877","name":"code-line","data":{},"nodes":[{"type":"text","id":"CgPZ-1705386952876","leaves":[{"text":"     db.Updateable(new Student() { Id = 1, SchoolId = 2, Name = \"jack2\" }).ExecuteCommand();","marks":[]}]}]},{"type":"block","id":"vXu3-1705386952879","name":"code-line","data":{},"nodes":[{"type":"text","id":"JyHO-1705386952878","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"ciLF-1705386952881","name":"code-line","data":{},"nodes":[{"type":"text","id":"7tB0-1705386952880","leaves":[{"text":"     //删除","marks":[]}]}]},{"type":"block","id":"cn8n-1705386952883","name":"code-line","data":{},"nodes":[{"type":"text","id":"jPSL-1705386952882","leaves":[{"text":"     db.Deleteable().Where(it => it.Id == 1).ExecuteCommand();","marks":[]}]}]},{"type":"block","id":"e8XW-1705386952885","name":"code-line","data":{},"nodes":[{"type":"text","id":"t7Wp-1705386952884","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"F5KJ-1705386952887","name":"code-line","data":{},"nodes":[{"type":"text","id":"Dx2H-1705386952886","leaves":[{"text":"     }","marks":[]}]}]},{"type":"block","id":"CyIl-1705386952889","name":"code-line","data":{},"nodes":[{"type":"text","id":"sxSD-1705386952888","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"Bdhn-1705386952891","name":"code-line","data":{},"nodes":[{"type":"text","id":"N4xE-1705386952890","leaves":[{"text":"       ","marks":[]}]}]},{"type":"block","id":"MHNr-1705386952893","name":"code-line","data":{},"nodes":[{"type":"text","id":"9qeH-1705386952892","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"fCDU-1705386952895","name":"code-line","data":{},"nodes":[{"type":"text","id":"A3eV-1705386952894","leaves":[{"text":"    //实体与数据库结构一样","marks":[]}]}]},{"type":"block","id":"bJS9-1705386952897","name":"code-line","data":{},"nodes":[{"type":"text","id":"gu3A-1705386952896","leaves":[{"text":"    public class Student","marks":[]}]}]},{"type":"block","id":"wY6p-1705386952899","name":"code-line","data":{},"nodes":[{"type":"text","id":"alIu-1705386952898","leaves":[{"text":"    {","marks":[]}]}]},{"type":"block","id":"HGXG-1705386952902","name":"code-line","data":{},"nodes":[{"type":"text","id":"kFt4-1705386952901","leaves":[{"text":"     //数据是自增需要加上IsIdentity ","marks":[]}]}]},{"type":"block","id":"j5hN-1705386952904","name":"code-line","data":{},"nodes":[{"type":"text","id":"cIPR-1705386952903","leaves":[{"text":"     //数据库是主键需要加上IsPrimaryKey ","marks":[]}]}]},{"type":"block","id":"5Xy5-1705386952906","name":"code-line","data":{},"nodes":[{"type":"text","id":"20gM-1705386952905","leaves":[{"text":"     //注意:要完全和数据库一致2个属性","marks":[]}]}]},{"type":"block","id":"PEAH-1705386952908","name":"code-line","data":{},"nodes":[{"type":"text","id":"101U-1705386952907","leaves":[{"text":"     [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]","marks":[]}]}]},{"type":"block","id":"MUzw-1705386952910","name":"code-line","data":{},"nodes":[{"type":"text","id":"vsNe-1705386952909","leaves":[{"text":"     public int Id { get; set; }","marks":[]}]}]},{"type":"block","id":"zM7B-1705386952912","name":"code-line","data":{},"nodes":[{"type":"text","id":"L50h-1705386952911","leaves":[{"text":"     public int? SchoolId { get; set; }","marks":[]}]}]},{"type":"block","id":"wgDp-1705386952914","name":"code-line","data":{},"nodes":[{"type":"text","id":"cPQG-1705386952913","leaves":[{"text":"     public string Name { get; set; }","marks":[]}]}]},{"type":"block","id":"iek6-1705386952916","name":"code-line","data":{},"nodes":[{"type":"text","id":"hojr-1705386952915","leaves":[{"text":"     }","marks":[]}]}]}]},{"type":"block","id":"MpDP-1705386952919","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"rPpP-1705386952918","leaves":[{"text":"1.2 偶发错误","marks":[]}]}]},{"type":"block","id":"D70B-1705386952921","name":"paragraph","data":{},"nodes":[{"type":"text","id":"JewY-1705386952920","leaves":[{"text":" SqlSugarClient ","marks":[{"type":"color","value":"#ff0000"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":14},{"type":"fontFamily","value":"Arial"}]},{"text":"出现偶发","marks":[{"type":"color","value":"#ff0000"}]},{"text":"错误全部换成","marks":[]},{"text":"SqlSugarScope进行测试","marks":[{"type":"color","value":"#ff0000"}]},{"text":",如果换了不报错了那么证明就是","marks":[]}]}]},{"type":"block","id":"re9n-1705386952923","name":"paragraph","data":{},"nodes":[{"type":"text","id":"3b1Z-1705386952922","leaves":[{"text":"线程安全问题,你也可以直接使用","marks":[]},{"text":"SqlSugarScope因为他是线程安全的 ,用SqlSugarScope要改成单例不然会有内存增加风险","marks":[{"type":"color","value":"#ff0000"}]}]}]},{"type":"block","id":"iOok-1705386952924","name":"paragraph","data":{},"nodes":[{"type":"text","id":"PI1F-1705386951726","leaves":[{"text":"详情:","marks":[{"type":"color","value":"#ff0000"}]}]},{"type":"inline","id":"jWB3-1705386951728","name":"link","data":{"href":"https://www.donet5.com/Home/Doc?typeId=1224"},"nodes":[{"type":"text","id":"PRnm-1705386951727","leaves":[{"text":"https://www.donet5.com/Home/Doc?typeId=1224","marks":[]}]}]},{"type":"text","id":"ly7u-1705386951729","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"EDZb-1705386952926","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ICLc-1705386952925","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"TfUP-1705386952928","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"ynDd-1705386952927","leaves":[{"text":"1.3  AddScope IOC","marks":[]}]}]},{"type":"block","id":"NCm1-1705386952930","name":"paragraph","data":{},"nodes":[{"type":"text","id":"pH43-1705386952929","leaves":[{"text":"Scope我们需要用SqlSugarClient","marks":[]}]}]},{"type":"block","id":"FWyg-1705386952999","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"jQOo-1705386952932","name":"code-line","data":{},"nodes":[{"type":"text","id":"xqQI-1705386952931","leaves":[{"text":"//注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行","marks":[]}]}]},{"type":"block","id":"q8va-1705386952934","name":"code-line","data":{},"nodes":[{"type":"text","id":"dRbG-1705386952933","leaves":[{"text":"services.AddHttpContextAccessor();","marks":[]}]}]},{"type":"block","id":"SXei-1705386952936","name":"code-line","data":{},"nodes":[{"type":"text","id":"Vbk0-1705386952935","leaves":[{"text":"//注册SqlSugar用AddScoped","marks":[]}]}]},{"type":"block","id":"Lb8M-1705386952938","name":"code-line","data":{},"nodes":[{"type":"text","id":"tCZl-1705386952937","leaves":[{"text":"services.AddScoped(s =>","marks":[]}]}]},{"type":"block","id":"ZtEl-1705386952940","name":"code-line","data":{},"nodes":[{"type":"text","id":"VXrc-1705386952939","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"l8Py-1705386952942","name":"code-line","data":{},"nodes":[{"type":"text","id":"Fbj3-1705386952941","leaves":[{"text":"        //Scoped用SqlSugarClient ","marks":[]}]}]},{"type":"block","id":"clqC-1705386952944","name":"code-line","data":{},"nodes":[{"type":"text","id":"kVgn-1705386952943","leaves":[{"text":"    SqlSugarClient sqlSugar = new SqlSugarClient (new ConnectionConfig()","marks":[]}]}]},{"type":"block","id":"T0VN-1705386952946","name":"code-line","data":{},"nodes":[{"type":"text","id":"rt6a-1705386952945","leaves":[{"text":"    {","marks":[]}]}]},{"type":"block","id":"422Y-1705386952948","name":"code-line","data":{},"nodes":[{"type":"text","id":"vXhf-1705386952947","leaves":[{"text":"        DbType = SqlSugar.DbType.Sqlite,","marks":[]}]}]},{"type":"block","id":"BJiq-1705386952950","name":"code-line","data":{},"nodes":[{"type":"text","id":"oVk0-1705386952949","leaves":[{"text":"        ConnectionString = \"DataSource=sqlsugar-dev.db\",","marks":[]}]}]},{"type":"block","id":"7hUN-1705386952952","name":"code-line","data":{},"nodes":[{"type":"text","id":"QtPr-1705386952951","leaves":[{"text":"        IsAutoCloseConnection = true,","marks":[]}]}]},{"type":"block","id":"P3gf-1705386952954","name":"code-line","data":{},"nodes":[{"type":"text","id":"j1Im-1705386952953","leaves":[{"text":"    },","marks":[]}]}]},{"type":"block","id":"W9Lx-1705386952956","name":"code-line","data":{},"nodes":[{"type":"text","id":"Zy0x-1705386952955","leaves":[{"text":"   db =>","marks":[]}]}]},{"type":"block","id":"9tRQ-1705386952958","name":"code-line","data":{},"nodes":[{"type":"text","id":"HJft-1705386952957","leaves":[{"text":"   {","marks":[]}]}]},{"type":"block","id":"Oqr2-1705386952960","name":"code-line","data":{},"nodes":[{"type":"text","id":"CdJA-1705386952959","leaves":[{"text":"      //每次上下文都会执行","marks":[]}]}]},{"type":"block","id":"DfPv-1705386952962","name":"code-line","data":{},"nodes":[{"type":"text","id":"QG31-1705386952961","leaves":[{"text":"           ","marks":[]}]}]},{"type":"block","id":"Bl6Z-1705386952964","name":"code-line","data":{},"nodes":[{"type":"text","id":"aj2C-1705386952963","leaves":[{"text":"      //获取IOC对象不要求在一个上下文","marks":[]}]}]},{"type":"block","id":"SzZD-1705386952966","name":"code-line","data":{},"nodes":[{"type":"text","id":"qoXs-1705386952965","leaves":[{"text":"      //vra log=s.GetService()","marks":[]}]}]},{"type":"block","id":"n7Hk-1705386952968","name":"code-line","data":{},"nodes":[{"type":"text","id":"Q7HA-1705386952967","leaves":[{"text":"               ","marks":[]}]}]},{"type":"block","id":"P7Sz-1705386952970","name":"code-line","data":{},"nodes":[{"type":"text","id":"wi4o-1705386952969","leaves":[{"text":"      //获取IOC对象要求在一个上下文","marks":[]}]}]},{"type":"block","id":"fcLO-1705386952972","name":"code-line","data":{},"nodes":[{"type":"text","id":"MiMx-1705386952971","leaves":[{"text":"      //var appServive = s.GetService();","marks":[]}]}]},{"type":"block","id":"xhMn-1705386952974","name":"code-line","data":{},"nodes":[{"type":"text","id":"WGUd-1705386952973","leaves":[{"text":"      //var log= appServive?.HttpContext?.RequestServices.GetService();","marks":[]}]}]},{"type":"block","id":"3yCs-1705386952976","name":"code-line","data":{},"nodes":[{"type":"text","id":"VQLQ-1705386952975","leaves":[{"text":"                ","marks":[]}]}]},{"type":"block","id":"wmne-1705386952978","name":"code-line","data":{},"nodes":[{"type":"text","id":"dZvT-1705386952977","leaves":[{"text":"       db.Aop.OnLogExecuting = (sql, pars) =>","marks":[]}]}]},{"type":"block","id":"KFMc-1705386952980","name":"code-line","data":{},"nodes":[{"type":"text","id":"CYVe-1705386952979","leaves":[{"text":"       {","marks":[]}]}]},{"type":"block","id":"z8i6-1705386952982","name":"code-line","data":{},"nodes":[{"type":"text","id":"o1gE-1705386952981","leaves":[{"text":"       ","marks":[]}]}]},{"type":"block","id":"Dzdj-1705386952984","name":"code-line","data":{},"nodes":[{"type":"text","id":"Ar2a-1705386952983","leaves":[{"text":"       };","marks":[]}]}]},{"type":"block","id":"l8V9-1705386952986","name":"code-line","data":{},"nodes":[{"type":"text","id":"bHFa-1705386952985","leaves":[{"text":"   });","marks":[]}]}]},{"type":"block","id":"8PYH-1705386952988","name":"code-line","data":{},"nodes":[{"type":"text","id":"z1xr-1705386952987","leaves":[{"text":"    return sqlSugar;","marks":[]}]}]},{"type":"block","id":"GqKC-1705386952990","name":"code-line","data":{},"nodes":[{"type":"text","id":"qJLj-1705386952989","leaves":[{"text":"});","marks":[]}]}]},{"type":"block","id":"A9d6-1705386952992","name":"code-line","data":{},"nodes":[{"type":"text","id":"OX26-1705386952991","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"JQx8-1705386952994","name":"code-line","data":{},"nodes":[{"type":"text","id":"bZ4E-1705386952993","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"2rc7-1705386952996","name":"code-line","data":{},"nodes":[{"type":"text","id":"fJQg-1705386952995","leaves":[{"text":"//用接口接收","marks":[]}]}]},{"type":"block","id":"Mclv-1705386952998","name":"code-line","data":{},"nodes":[{"type":"text","id":"BZd3-1705386952997","leaves":[{"text":"public class(ISqlSugarClient  db)","marks":[]}]}]}]},{"type":"block","id":"FvO4-1705386953001","name":"paragraph","data":{},"nodes":[{"type":"text","id":"QvGb-1705386953000","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"Jj6G-1705386953003","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"sQqN-1705386953002","leaves":[{"text":"2、单例模式:SqlSugarScope ","marks":[]}]}]},{"type":"block","id":"VI6I-1705386953005","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"e1bT-1705386953004","leaves":[{"text":"2.1 完整用例","marks":[]}]}]},{"type":"block","id":"u2G0-1705386953007","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Hcp5-1705386953006","leaves":[{"text":"SqlSugarScope 请使用单例模式,配置参数有2种周期","marks":[]}]}]},{"type":"block","id":"t4y4-1705386953009","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Grb5-1705386953008","leaves":[{"text":"(A):全局生效","marks":[]}]}]},{"type":"block","id":"dTRo-1705386953011","name":"paragraph","data":{},"nodes":[{"type":"text","id":"iWMI-1705386953010","leaves":[{"text":"(B):   当前上下文生效","marks":[]}]}]},{"type":"block","id":"v6m0-1705386953149","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"A7LB-1705386953013","name":"code-line","data":{},"nodes":[{"type":"text","id":"PSMR-1705386953012","leaves":[{"text":" public  class  SqlSugarHelper //不能是泛型类","marks":[]}]}]},{"type":"block","id":"Bj4C-1705386953015","name":"code-line","data":{},"nodes":[{"type":"text","id":"Y9do-1705386953014","leaves":[{"text":" {","marks":[]}]}]},{"type":"block","id":"34YV-1705386953017","name":"code-line","data":{},"nodes":[{"type":"text","id":"lCEn-1705386953016","leaves":[{"text":" ","marks":[]}]}]},{"type":"block","id":"2Ozo-1705386953019","name":"code-line","data":{},"nodes":[{"type":"text","id":"lUyU-1705386953018","leaves":[{"text":"    //多库情况下使用说明:","marks":[]}]}]},{"type":"block","id":"Yejh-1705386953021","name":"code-line","data":{},"nodes":[{"type":"text","id":"ckT7-1705386953020","leaves":[{"text":"    //如果是固定多库可以传 new SqlSugarScope(List,db=>{}) 文档:多租户","marks":[]}]}]},{"type":"block","id":"2xQ8-1705386953023","name":"code-line","data":{},"nodes":[{"type":"text","id":"OIyg-1705386953022","leaves":[{"text":"    //如果是不固定多库 可以看文档Saas分库","marks":[]}]}]},{"type":"block","id":"zTIr-1705386953025","name":"code-line","data":{},"nodes":[{"type":"text","id":"Al8S-1705386953024","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"4ukk-1705386953027","name":"code-line","data":{},"nodes":[{"type":"text","id":"JWYt-1705386953026","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"0Joc-1705386953029","name":"code-line","data":{},"nodes":[{"type":"text","id":"N25j-1705386953028","leaves":[{"text":"   //用单例模式","marks":[]}]}]},{"type":"block","id":"Y9a8-1705386953031","name":"code-line","data":{},"nodes":[{"type":"text","id":"iXqC-1705386953030","leaves":[{"text":"    public static SqlSugarScope Db= new SqlSugarScope(new ConnectionConfig()","marks":[]}]}]},{"type":"block","id":"f4eN-1705386953033","name":"code-line","data":{},"nodes":[{"type":"text","id":"JCLm-1705386953032","leaves":[{"text":"   {","marks":[]}]}]},{"type":"block","id":"h8YN-1705386953036","name":"code-line","data":{},"nodes":[{"type":"text","id":"Z8fj-1705386953035","leaves":[{"text":"            ConnectionString = \"Server=.xxxxx\",//连接符字串","marks":[]}]}]},{"type":"block","id":"GpgX-1705386953038","name":"code-line","data":{},"nodes":[{"type":"text","id":"9OCq-1705386953037","leaves":[{"text":"            DbType = DbType.SqlServer,//数据库类型","marks":[]}]}]},{"type":"block","id":"EMXh-1705386953040","name":"code-line","data":{},"nodes":[{"type":"text","id":"cj18-1705386953039","leaves":[{"text":"            IsAutoCloseConnection = true //不设成true要手动close","marks":[]}]}]},{"type":"block","id":"Ql8K-1705386953042","name":"code-line","data":{},"nodes":[{"type":"text","id":"SYdA-1705386953041","leaves":[{"text":"   },","marks":[]}]}]},{"type":"block","id":"Okt7-1705386953044","name":"code-line","data":{},"nodes":[{"type":"text","id":"9OFX-1705386953043","leaves":[{"text":"  db=> {","marks":[]}]}]},{"type":"block","id":"gcg7-1705386953046","name":"code-line","data":{},"nodes":[{"type":"text","id":"Dtq9-1705386953045","leaves":[{"text":"         //(A)全局生效配置点,一般AOP和程序启动的配置扔这里面 ,所有上下文生效","marks":[]}]}]},{"type":"block","id":"OkSE-1705386953048","name":"code-line","data":{},"nodes":[{"type":"text","id":"yqlp-1705386953047","leaves":[{"text":"         //调试SQL事件,可以删掉","marks":[]}]}]},{"type":"block","id":"RHML-1705386953050","name":"code-line","data":{},"nodes":[{"type":"text","id":"20nT-1705386953049","leaves":[{"text":"         db.Aop.OnLogExecuting = (sql, pars) =>","marks":[]}]}]},{"type":"block","id":"dyzD-1705386953052","name":"code-line","data":{},"nodes":[{"type":"text","id":"FBFI-1705386953051","leaves":[{"text":"         {","marks":[]}]}]},{"type":"block","id":"tskL-1705386953054","name":"code-line","data":{},"nodes":[{"type":"text","id":"IvPt-1705386953053","leaves":[{"text":"          Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响","marks":[]}]}]},{"type":"block","id":"tibr-1705386953056","name":"code-line","data":{},"nodes":[{"type":"text","id":"bSY1-1705386953055","leaves":[{"text":"          ","marks":[]}]}]},{"type":"block","id":"Pgjd-1705386953058","name":"code-line","data":{},"nodes":[{"type":"text","id":"Tgtu-1705386953057","leaves":[{"text":"           //获取原生SQL推荐 5.1.4.63  性能OK","marks":[]}]}]},{"type":"block","id":"P9Bd-1705386953060","name":"code-line","data":{},"nodes":[{"type":"text","id":"5KYh-1705386953059","leaves":[{"text":"           //UtilMethods.GetNativeSql(sql,pars)","marks":[]}]}]},{"type":"block","id":"XcbT-1705386953062","name":"code-line","data":{},"nodes":[{"type":"text","id":"QBxw-1705386953061","leaves":[{"text":"                               ","marks":[]}]}]},{"type":"block","id":"OYuX-1705386953064","name":"code-line","data":{},"nodes":[{"type":"text","id":"7WKu-1705386953063","leaves":[{"text":"           //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用","marks":[]}]}]},{"type":"block","id":"KVsa-1705386953066","name":"code-line","data":{},"nodes":[{"type":"text","id":"wYBr-1705386953065","leaves":[{"text":"           //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)","marks":[]}]}]},{"type":"block","id":"qt2z-1705386953068","name":"code-line","data":{},"nodes":[{"type":"text","id":"zTVr-1705386953067","leaves":[{"text":"                    ","marks":[]}]}]},{"type":"block","id":"uIyh-1705386953070","name":"code-line","data":{},"nodes":[{"type":"text","id":"ET30-1705386953069","leaves":[{"text":"         };","marks":[]}]}]},{"type":"block","id":"hTEz-1705386953072","name":"code-line","data":{},"nodes":[{"type":"text","id":"x6FV-1705386953071","leaves":[{"text":"         ","marks":[]}]}]},{"type":"block","id":"dFqi-1705386953074","name":"code-line","data":{},"nodes":[{"type":"text","id":"huLu-1705386953073","leaves":[{"text":"         //多个配置就写下面","marks":[]}]}]},{"type":"block","id":"v9V7-1705386953076","name":"code-line","data":{},"nodes":[{"type":"text","id":"7Lka-1705386953075","leaves":[{"text":"         //db.Ado.IsDisableMasterSlaveSeparation=true;","marks":[]}]}]},{"type":"block","id":"hRFk-1705386953078","name":"code-line","data":{},"nodes":[{"type":"text","id":"THcQ-1705386953077","leaves":[{"text":"         ","marks":[]}]}]},{"type":"block","id":"7dD4-1705386953080","name":"code-line","data":{},"nodes":[{"type":"text","id":"DXIX-1705386953079","leaves":[{"text":"          //注意多租户 有几个设置几个","marks":[]}]}]},{"type":"block","id":"zso7-1705386953082","name":"code-line","data":{},"nodes":[{"type":"text","id":"d8fd-1705386953081","leaves":[{"text":"          //db.GetConnection(i).Aop","marks":[]}]}]},{"type":"block","id":"8axP-1705386953084","name":"code-line","data":{},"nodes":[{"type":"text","id":"KnjU-1705386953083","leaves":[{"text":"     });","marks":[]}]}]},{"type":"block","id":"J2xw-1705386953086","name":"code-line","data":{},"nodes":[{"type":"text","id":"b8WL-1705386953085","leaves":[{"text":"   }","marks":[]}]}]},{"type":"block","id":"kzPi-1705386953088","name":"code-line","data":{},"nodes":[{"type":"text","id":"kjny-1705386953087","leaves":[{"text":"   //(B)当前上下文生效配置点,一般多租户相关操作写在这儿 ","marks":[]}]}]},{"type":"block","id":"2V5q-1705386953090","name":"code-line","data":{},"nodes":[{"type":"text","id":"Ko0i-1705386953089","leaves":[{"text":"   //db.GetConnectionxxx 只在当前上下文有效,不会共享","marks":[]}]}]},{"type":"block","id":"qqB3-1705386953092","name":"code-line","data":{},"nodes":[{"type":"text","id":"JP1W-1705386953091","leaves":[{"text":"   //如果是程序启动时的配置全部到A里面去","marks":[]}]}]},{"type":"block","id":"loFq-1705386953094","name":"code-line","data":{},"nodes":[{"type":"text","id":"99UC-1705386953093","leaves":[{"text":"  ","marks":[]}]}]},{"type":"block","id":"pa4T-1705386953096","name":"code-line","data":{},"nodes":[{"type":"text","id":"1qY4-1705386953095","leaves":[{"text":"   //建表","marks":[]}]}]},{"type":"block","id":"bt5X-1705386953098","name":"code-line","data":{},"nodes":[{"type":"text","id":"e1jU-1705386953097","leaves":[{"text":"  //db.CodeFirst.InitTables(); 更多看文档迁移   ","marks":[]}]}]},{"type":"block","id":"jDef-1705386953100","name":"code-line","data":{},"nodes":[{"type":"text","id":"q3te-1705386953099","leaves":[{"text":"  ","marks":[]}]}]},{"type":"block","id":"wM4O-1705386953102","name":"code-line","data":{},"nodes":[{"type":"text","id":"ml7x-1705386953101","leaves":[{"text":"  //查询表的所有","marks":[]}]}]},{"type":"block","id":"T6nV-1705386953104","name":"code-line","data":{},"nodes":[{"type":"text","id":"IFCj-1705386953103","leaves":[{"text":"  var list=SqlSugarHelper.Db.Queryable().ToList();","marks":[]}]}]},{"type":"block","id":"oAzp-1705386953106","name":"code-line","data":{},"nodes":[{"type":"text","id":"7KaE-1705386953105","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"Ms46-1705386953108","name":"code-line","data":{},"nodes":[{"type":"text","id":"hrpf-1705386953107","leaves":[{"text":"  //插入","marks":[]}]}]},{"type":"block","id":"dfTa-1705386953110","name":"code-line","data":{},"nodes":[{"type":"text","id":"pbFP-1705386953109","leaves":[{"text":"  SqlSugarHelper.Db.Insertable(new Student(){ SchoolId = 1, Name = \"jack\" }).ExecuteCommand();","marks":[]}]}]},{"type":"block","id":"dvBq-1705386953112","name":"code-line","data":{},"nodes":[{"type":"text","id":"qD4b-1705386953111","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"qUIh-1705386953114","name":"code-line","data":{},"nodes":[{"type":"text","id":"MB8C-1705386953113","leaves":[{"text":"  //更新","marks":[]}]}]},{"type":"block","id":"dyMu-1705386953116","name":"code-line","data":{},"nodes":[{"type":"text","id":"tPcI-1705386953115","leaves":[{"text":"  SqlSugarHelper.Db.Updateable(new Student(){ Id = 1,SchoolId=2,Name=\"jack2\" }).ExecuteCommand();","marks":[]}]}]},{"type":"block","id":"AX4R-1705386953118","name":"code-line","data":{},"nodes":[{"type":"text","id":"tK95-1705386953117","leaves":[{"text":"     ","marks":[]}]}]},{"type":"block","id":"MVAw-1705386953120","name":"code-line","data":{},"nodes":[{"type":"text","id":"YzrG-1705386953119","leaves":[{"text":"  //删除","marks":[]}]}]},{"type":"block","id":"gdPb-1705386953122","name":"code-line","data":{},"nodes":[{"type":"text","id":"TE9f-1705386953121","leaves":[{"text":"  SqlSugarHelper.Db.Deleteable().Where(it => it.Id == 1).ExecuteCommand();","marks":[]}]}]},{"type":"block","id":"HHzN-1705386953124","name":"code-line","data":{},"nodes":[{"type":"text","id":"DLCV-1705386953123","leaves":[{"text":"  ","marks":[]}]}]},{"type":"block","id":"pHfs-1705386953126","name":"code-line","data":{},"nodes":[{"type":"text","id":"BgDR-1705386953125","leaves":[{"text":"  ","marks":[]}]}]},{"type":"block","id":"J2U7-1705386953128","name":"code-line","data":{},"nodes":[{"type":"text","id":"NxEY-1705386953127","leaves":[{"text":"   //实体与数据库结构一样","marks":[]}]}]},{"type":"block","id":"e86i-1705386953130","name":"code-line","data":{},"nodes":[{"type":"text","id":"ksZs-1705386953129","leaves":[{"text":"   public class Student","marks":[]}]}]},{"type":"block","id":"nsxc-1705386953132","name":"code-line","data":{},"nodes":[{"type":"text","id":"0JIp-1705386953131","leaves":[{"text":"   {","marks":[]}]}]},{"type":"block","id":"QjSd-1705386953134","name":"code-line","data":{},"nodes":[{"type":"text","id":"qzws-1705386953133","leaves":[{"text":"     //数据是自增需要加上IsIdentity ","marks":[]}]}]},{"type":"block","id":"eAvF-1705386953136","name":"code-line","data":{},"nodes":[{"type":"text","id":"wmDR-1705386953135","leaves":[{"text":"     //数据库是主键需要加上IsPrimaryKey ","marks":[]}]}]},{"type":"block","id":"5dAT-1705386953138","name":"code-line","data":{},"nodes":[{"type":"text","id":"HFTz-1705386953137","leaves":[{"text":"     //注意:要完全和数据库一致2个属性","marks":[]}]}]},{"type":"block","id":"wBCZ-1705386953140","name":"code-line","data":{},"nodes":[{"type":"text","id":"csQ8-1705386953139","leaves":[{"text":"     [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]","marks":[]}]}]},{"type":"block","id":"7hNS-1705386953142","name":"code-line","data":{},"nodes":[{"type":"text","id":"UZaQ-1705386953141","leaves":[{"text":"     public int Id { get; set; }","marks":[]}]}]},{"type":"block","id":"5UU8-1705386953144","name":"code-line","data":{},"nodes":[{"type":"text","id":"1cGI-1705386953143","leaves":[{"text":"     public int? SchoolId { get; set; }","marks":[]}]}]},{"type":"block","id":"pRt7-1705386953146","name":"code-line","data":{},"nodes":[{"type":"text","id":"ehh0-1705386953145","leaves":[{"text":"     public string Name { get; set; }","marks":[]}]}]},{"type":"block","id":"1M6d-1705386953148","name":"code-line","data":{},"nodes":[{"type":"text","id":"hxSu-1705386953147","leaves":[{"text":"   }","marks":[]}]}]}]},{"type":"block","id":"qc52-1705386953151","name":"paragraph","data":{},"nodes":[{"type":"text","id":"8rJ5-1705386953150","leaves":[{"text":"验证单例是否成功:","marks":[{"type":"color","value":"#ff0000"}]}]}]},{"type":"block","id":"nZfY-1705386953156","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"DWjV-1705386953153","name":"code-line","data":{},"nodes":[{"type":"text","id":"6SbQ-1705386953152","leaves":[{"text":" SqlSugarHelper.Db.HasCode();//只要看这个hascode在服务启动后都一个那么说明成功了","marks":[]}]}]},{"type":"block","id":"VPPY-1705386953155","name":"code-line","data":{},"nodes":[{"type":"text","id":"bJ5q-1705386953154","leaves":[{"text":" //IOC直接复制我的DEMO就不需要验证","marks":[]}]}]}]},{"type":"block","id":"T5GX-1705386953158","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"elPO-1705386953157","leaves":[{"text":"2.2 偶发错误 ","marks":[]}]}]},{"type":"block","id":"7No1-1705386953159","name":"paragraph","data":{},"nodes":[{"type":"text","id":"0tkY-1705386951847","leaves":[{"text":"详情:","marks":[{"type":"color","value":"#ff0000"}]}]},{"type":"inline","id":"X7P8-1705386951849","name":"link","data":{"href":"https://www.donet5.com/Home/Doc?typeId=1224"},"nodes":[{"type":"text","id":"rGyF-1705386951848","leaves":[{"text":"https://www.donet5.com/Home/Doc?typeId=1224","marks":[]}]}]},{"type":"text","id":"ACIj-1705386951850","leaves":[{"text":"","marks":[{"type":"color","value":"#ff0000"}]}]}]},{"type":"block","id":"SFPa-1705386953161","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"BP7E-1705386953160","leaves":[{"text":"2.4 原理","marks":[]}]}]},{"type":"block","id":"PAMH-1705386953163","name":"paragraph","data":{},"nodes":[{"type":"inline","id":"vfKa-1705386951852","name":"link","data":{"href":"https://www.donet5.com/Home/Doc?typeId=2352"},"nodes":[{"type":"text","id":"eHdx-1705386953162","leaves":[{"text":"https://www.donet5.com/Home/Doc?typeId=2352","marks":[]}]}]}]},{"type":"block","id":"tJ4Z-1705386953165","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"PEdf-1705386953164","leaves":[{"text":"2.5  AddSingleton IOC","marks":[]}]}]},{"type":"block","id":"QhQo-1705386953167","name":"paragraph","data":{},"nodes":[{"type":"text","id":"cSyh-1705386953166","leaves":[{"text":"AddSingleton 我们需要用SqlSugarScope单例对象","marks":[]}]}]},{"type":"block","id":"CXaA-1705386953233","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"8R7v-1705386953170","name":"code-line","data":{},"nodes":[{"type":"text","id":"6iIZ-1705386953169","leaves":[{"text":"//注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行","marks":[]}]}]},{"type":"block","id":"SQ7L-1705386953172","name":"code-line","data":{},"nodes":[{"type":"text","id":"oA39-1705386953171","leaves":[{"text":"services.AddHttpContextAccessor();","marks":[]}]}]},{"type":"block","id":"vlXc-1705386953174","name":"code-line","data":{},"nodes":[{"type":"text","id":"Iy1P-1705386953173","leaves":[{"text":"//注册SqlSugar","marks":[]}]}]},{"type":"block","id":"Alju-1705386953176","name":"code-line","data":{},"nodes":[{"type":"text","id":"59Kt-1705386953175","leaves":[{"text":"services.AddSingleton(s =>","marks":[]}]}]},{"type":"block","id":"4Eua-1705386953178","name":"code-line","data":{},"nodes":[{"type":"text","id":"1JiZ-1705386953177","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"kcU2-1705386953180","name":"code-line","data":{},"nodes":[{"type":"text","id":"YAX3-1705386953179","leaves":[{"text":"    SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()","marks":[]}]}]},{"type":"block","id":"Jrl4-1705386953182","name":"code-line","data":{},"nodes":[{"type":"text","id":"1Hk5-1705386953181","leaves":[{"text":"    {","marks":[]}]}]},{"type":"block","id":"YO4V-1705386953184","name":"code-line","data":{},"nodes":[{"type":"text","id":"D8mq-1705386953183","leaves":[{"text":"        DbType = SqlSugar.DbType.Sqlite,","marks":[]}]}]},{"type":"block","id":"epkQ-1705386953186","name":"code-line","data":{},"nodes":[{"type":"text","id":"9mYM-1705386953185","leaves":[{"text":"        ConnectionString = \"DataSource=sqlsugar-dev.db\",","marks":[]}]}]},{"type":"block","id":"fpoz-1705386953188","name":"code-line","data":{},"nodes":[{"type":"text","id":"S7Lo-1705386953187","leaves":[{"text":"        IsAutoCloseConnection = true,","marks":[]}]}]},{"type":"block","id":"7OKJ-1705386953190","name":"code-line","data":{},"nodes":[{"type":"text","id":"gNmv-1705386953189","leaves":[{"text":"    },","marks":[]}]}]},{"type":"block","id":"curd-1705386953192","name":"code-line","data":{},"nodes":[{"type":"text","id":"sQVO-1705386953191","leaves":[{"text":"   db =>","marks":[]}]}]},{"type":"block","id":"G09K-1705386953194","name":"code-line","data":{},"nodes":[{"type":"text","id":"hBD4-1705386953193","leaves":[{"text":"   {         ","marks":[]}]}]},{"type":"block","id":"V24R-1705386953196","name":"code-line","data":{},"nodes":[{"type":"text","id":"80Y8-1705386953195","leaves":[{"text":"      //每次上下文都会执行","marks":[]}]}]},{"type":"block","id":"cSu3-1705386953198","name":"code-line","data":{},"nodes":[{"type":"text","id":"Kza9-1705386953197","leaves":[{"text":"           ","marks":[]}]}]},{"type":"block","id":"JwkV-1705386953200","name":"code-line","data":{},"nodes":[{"type":"text","id":"YpJr-1705386953199","leaves":[{"text":"      //获取IOC对象不要求在一个上下文","marks":[]}]}]},{"type":"block","id":"zJfh-1705386953202","name":"code-line","data":{},"nodes":[{"type":"text","id":"xYr8-1705386953201","leaves":[{"text":"      //vra log=s.GetService()","marks":[]}]}]},{"type":"block","id":"ilHx-1705386953204","name":"code-line","data":{},"nodes":[{"type":"text","id":"BnUu-1705386953203","leaves":[{"text":"               ","marks":[]}]}]},{"type":"block","id":"flbU-1705386953206","name":"code-line","data":{},"nodes":[{"type":"text","id":"7tA5-1705386953205","leaves":[{"text":"      //获取IOC对象要求在一个上下文","marks":[]}]}]},{"type":"block","id":"6dVr-1705386953208","name":"code-line","data":{},"nodes":[{"type":"text","id":"wlpb-1705386953207","leaves":[{"text":"      //var appServive = s.GetService();","marks":[]}]}]},{"type":"block","id":"VfAS-1705386953210","name":"code-line","data":{},"nodes":[{"type":"text","id":"5DB3-1705386953209","leaves":[{"text":"      //var log= appServive?.HttpContext?.RequestServices.GetService();","marks":[]}]}]},{"type":"block","id":"ktyW-1705386953212","name":"code-line","data":{},"nodes":[{"type":"text","id":"pZWE-1705386953211","leaves":[{"text":"                ","marks":[]}]}]},{"type":"block","id":"Qizk-1705386953214","name":"code-line","data":{},"nodes":[{"type":"text","id":"wAhx-1705386953213","leaves":[{"text":"       db.Aop.OnLogExecuting = (sql, pars) =>","marks":[]}]}]},{"type":"block","id":"s2zI-1705386953216","name":"code-line","data":{},"nodes":[{"type":"text","id":"aLGv-1705386953215","leaves":[{"text":"       {","marks":[]}]}]},{"type":"block","id":"k1Bb-1705386953218","name":"code-line","data":{},"nodes":[{"type":"text","id":"07kT-1705386953217","leaves":[{"text":"       ","marks":[]}]}]},{"type":"block","id":"esHi-1705386953220","name":"code-line","data":{},"nodes":[{"type":"text","id":"nWos-1705386953219","leaves":[{"text":"       };","marks":[]}]}]},{"type":"block","id":"Z8aU-1705386953222","name":"code-line","data":{},"nodes":[{"type":"text","id":"mwxL-1705386953221","leaves":[{"text":"   });","marks":[]}]}]},{"type":"block","id":"BNBC-1705386953224","name":"code-line","data":{},"nodes":[{"type":"text","id":"SUMn-1705386953223","leaves":[{"text":"    return sqlSugar;","marks":[]}]}]},{"type":"block","id":"XsCQ-1705386953226","name":"code-line","data":{},"nodes":[{"type":"text","id":"vyVl-1705386953225","leaves":[{"text":"});","marks":[]}]}]},{"type":"block","id":"mZFI-1705386953228","name":"code-line","data":{},"nodes":[{"type":"text","id":"28cy-1705386953227","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"e4ZN-1705386953230","name":"code-line","data":{},"nodes":[{"type":"text","id":"WDxc-1705386953229","leaves":[{"text":"//用接口接收","marks":[]}]}]},{"type":"block","id":"tSrM-1705386953232","name":"code-line","data":{},"nodes":[{"type":"text","id":"GaTq-1705386953231","leaves":[{"text":"public class(ISqlSugarClient  db)","marks":[]}]}]}]},{"type":"block","id":"JNIF-1705386953235","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Bbz9-1705386953234","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"9kY7-1705386953237","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"mnZx-1705386953236","leaves":[{"text":"多库用法 ","marks":[]}]}]},{"type":"block","id":"2Yae-1705386953252","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"z3cm-1705386953239","name":"code-line","data":{},"nodes":[{"type":"text","id":"lZ1S-1705386953238","leaves":[{"text":"var db = new SqlSugarClient(new List()","marks":[]}]}]},{"type":"block","id":"SKhm-1705386953241","name":"code-line","data":{},"nodes":[{"type":"text","id":"7Wak-1705386953240","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"Edp5-1705386953243","name":"code-line","data":{},"nodes":[{"type":"text","id":"OBA9-1705386953242","leaves":[{"text":" new ConnectionConfig(){ConfigId=\"0\",DbType=DbType.SqlServer,ConnectionString=..,IsAutoCloseConnection=true},","marks":[]}]}]},{"type":"block","id":"buYv-1705386953245","name":"code-line","data":{},"nodes":[{"type":"text","id":"9tGO-1705386953244","leaves":[{"text":" new ConnectionConfig(){ConfigId=\"1\",DbType=DbType.MySql,ConnectionString=..,IsAutoCloseConnection=true  }","marks":[]}]}]},{"type":"block","id":"pDwb-1705386953247","name":"code-line","data":{},"nodes":[{"type":"text","id":"pZwx-1705386953246","leaves":[{"text":"});","marks":[]}]}]},{"type":"block","id":"Ntlb-1705386953249","name":"code-line","data":{},"nodes":[{"type":"text","id":"nEwt-1705386953248","leaves":[{"text":"var childA=db.GetConnection(\"A\");","marks":[]}]}]},{"type":"block","id":"xqmh-1705386953251","name":"code-line","data":{},"nodes":[{"type":"text","id":"Tifa-1705386953250","leaves":[{"text":"var childB=db.GetConnection(\"B\");","marks":[]}]}]}]},{"type":"block","id":"GRua-1705386953255","name":"paragraph","data":{},"nodes":[{"type":"text","id":"bzRn-1705386953253","leaves":[{"text":"详细教程:","marks":[]}]},{"type":"inline","id":"adez-1705386951899","name":"link","data":{"href":"https://www.donet5.com/Home/Doc?typeId=2246"},"nodes":[{"type":"text","id":"V2na-1705386953254","leaves":[{"text":"https://www.donet5.com/Home/Doc?typeId=2246","marks":[]}]}]}]},{"type":"block","id":"Qh8C-1705386953257","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Rxqg-1705386953256","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"P7qP-1705386953259","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"C5aU-1705386953258","leaves":[{"text":"连接参数","marks":[]}]}]},{"type":"block","id":"cHJM-1705386953261","name":"paragraph","data":{},"nodes":[{"type":"text","id":"CBFo-1705386953260","leaves":[{"text":"SqlSugarClient是通过ConnectionConfig进行传参数详细参数如下","marks":[]}]}]},{"type":"block","id":"0vMy-1705386953343","name":"table","data":{"colsWidth":[220.66666666666666,220.66666666666666,220.66666666666666],"rowsHeight":[40,40,40,40,40,40,40,40]},"nodes":[{"type":"block","id":"nHDn-1705386953271","name":"table-row","data":{},"nodes":[{"type":"block","id":"ciiv-1705386953264","name":"table-cell","data":{},"nodes":[{"type":"block","id":"6vSX-1705386953263","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Odp1-1705386953262","leaves":[{"text":"名称","marks":[]}]}]}]},{"type":"block","id":"jlHJ-1705386953267","name":"table-cell","data":{},"nodes":[{"type":"block","id":"5bW1-1705386953266","name":"paragraph","data":{},"nodes":[{"type":"text","id":"0fJR-1705386953265","leaves":[{"text":"描述","marks":[]}]}]}]},{"type":"block","id":"dRsx-1705386953270","name":"table-cell","data":{},"nodes":[{"type":"block","id":"ga9k-1705386953269","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ycAt-1705386953268","leaves":[{"text":"必填","marks":[]}]}]}]}]},{"type":"block","id":"C5bW-1705386953281","name":"table-row","data":{},"nodes":[{"type":"block","id":"7GYS-1705386953274","name":"table-cell","data":{},"nodes":[{"type":"block","id":"AbVl-1705386953273","name":"paragraph","data":{},"nodes":[{"type":"text","id":"kBz7-1705386953272","leaves":[{"text":"DbType","marks":[]}]}]}]},{"type":"block","id":"7fT3-1705386953277","name":"table-cell","data":{},"nodes":[{"type":"block","id":"S4aX-1705386953276","name":"paragraph","data":{},"nodes":[{"type":"text","id":"VxZE-1705386953275","leaves":[{"text":"数据库类型","marks":[]}]}]}]},{"type":"block","id":"fqW6-1705386953280","name":"table-cell","data":{},"nodes":[{"type":"block","id":"1Ryo-1705386953279","name":"paragraph","data":{},"nodes":[{"type":"text","id":"tds5-1705386953278","leaves":[{"text":"是","marks":[]}]}]}]}]},{"type":"block","id":"zg3N-1705386953291","name":"table-row","data":{},"nodes":[{"type":"block","id":"f2Qi-1705386953284","name":"table-cell","data":{},"nodes":[{"type":"block","id":"JpbA-1705386953283","name":"paragraph","data":{},"nodes":[{"type":"text","id":"R2Md-1705386953282","leaves":[{"text":"ConnectionString","marks":[]}]}]}]},{"type":"block","id":"6ZtW-1705386953287","name":"table-cell","data":{},"nodes":[{"type":"block","id":"0vfL-1705386953286","name":"paragraph","data":{},"nodes":[{"type":"text","id":"XeRs-1705386953285","leaves":[{"text":"连接字符串","marks":[]}]}]}]},{"type":"block","id":"xd0O-1705386953290","name":"table-cell","data":{},"nodes":[{"type":"block","id":"DX8x-1705386953289","name":"paragraph","data":{},"nodes":[{"type":"text","id":"4I7h-1705386953288","leaves":[{"text":"是","marks":[]}]}]}]}]},{"type":"block","id":"hHNc-1705386953301","name":"table-row","data":{},"nodes":[{"type":"block","id":"L0wK-1705386953294","name":"table-cell","data":{},"nodes":[{"type":"block","id":"APYa-1705386953293","name":"paragraph","data":{},"nodes":[{"type":"text","id":"n9TQ-1705386953292","leaves":[{"text":"IsAutoCloseConnection","marks":[]}]}]}]},{"type":"block","id":"bbOm-1705386953297","name":"table-cell","data":{},"nodes":[{"type":"block","id":"bUs6-1705386953296","name":"paragraph","data":{},"nodes":[{"type":"text","id":"0Ngb-1705386953295","leaves":[{"text":"自动释放和关闭数据库连接,如果有事务事务结束时关闭,否则每次操作后关闭","marks":[]}]}]}]},{"type":"block","id":"Lpau-1705386953300","name":"table-cell","data":{},"nodes":[{"type":"block","id":"cMIO-1705386953299","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ktu0-1705386953298","leaves":[{"text":"","marks":[]}]}]}]}]},{"type":"block","id":"QXca-1705386953311","name":"table-row","data":{},"nodes":[{"type":"block","id":"I3sY-1705386953304","name":"table-cell","data":{},"nodes":[{"type":"block","id":"nFvC-1705386953303","name":"paragraph","data":{},"nodes":[{"type":"text","id":"GVIi-1705386953302","leaves":[{"text":"ConfigureExternalServices","marks":[]}]}]}]},{"type":"block","id":"mdV3-1705386953307","name":"table-cell","data":{},"nodes":[{"type":"block","id":"y2bI-1705386953306","name":"paragraph","data":{},"nodes":[{"type":"text","id":"7s9R-1705386953305","leaves":[{"text":"一些扩展层务的集成","marks":[]}]}]}]},{"type":"block","id":"427U-1705386953310","name":"table-cell","data":{},"nodes":[{"type":"block","id":"Kldr-1705386953309","name":"paragraph","data":{},"nodes":[{"type":"text","id":"EleO-1705386953308","leaves":[{"text":"","marks":[]}]}]}]}]},{"type":"block","id":"ipzy-1705386953322","name":"table-row","data":{},"nodes":[{"type":"block","id":"Of0T-1705386953314","name":"table-cell","data":{},"nodes":[{"type":"block","id":"szAM-1705386953313","name":"paragraph","data":{},"nodes":[{"type":"text","id":"xoaH-1705386953312","leaves":[{"text":"MoreSettings","marks":[]}]}]}]},{"type":"block","id":"ZrnD-1705386953317","name":"table-cell","data":{},"nodes":[{"type":"block","id":"z3MU-1705386953316","name":"list-item","data":{"listId":"Ux3X-1705386951333","listLevel":1,"listType":"unordered"},"nodes":[{"type":"text","id":"Zxg4-1705386953315","leaves":[{"text":"点击查看","marks":[]}]}]}]},{"type":"block","id":"R5Oz-1705386953321","name":"table-cell","data":{},"nodes":[{"type":"block","id":"j3cg-1705386953319","name":"paragraph","data":{},"nodes":[{"type":"text","id":"gVr3-1705386953318","leaves":[{"text":"","marks":[]}]}]}]}]},{"type":"block","id":"pRYz-1705386953332","name":"table-row","data":{},"nodes":[{"type":"block","id":"WRnw-1705386953325","name":"table-cell","data":{},"nodes":[{"type":"block","id":"65MR-1705386953324","name":"paragraph","data":{},"nodes":[{"type":"text","id":"s72h-1705386953323","leaves":[{"text":"SlaveConnectionConfigs","marks":[]}]}]}]},{"type":"block","id":"3keI-1705386953328","name":"table-cell","data":{},"nodes":[{"type":"block","id":"KF4W-1705386953327","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ZcCR-1705386953326","leaves":[{"text":"主从设置","marks":[]}]}]}]},{"type":"block","id":"bXc5-1705386953331","name":"table-cell","data":{},"nodes":[{"type":"block","id":"96q7-1705386953330","name":"paragraph","data":{},"nodes":[{"type":"text","id":"C1gV-1705386953329","leaves":[{"text":"","marks":[]}]}]}]}]},{"type":"block","id":"hdLL-1705386953342","name":"table-row","data":{},"nodes":[{"type":"block","id":"DeAF-1705386953335","name":"table-cell","data":{"colSpan":1,"rowSpan":1},"nodes":[{"type":"block","id":"4zE6-1705386953334","name":"paragraph","data":{},"nodes":[{"type":"text","id":"KvKh-1705386953333","leaves":[{"text":"LanguageType","marks":[]}]}]}]},{"type":"block","id":"bbeu-1705386953338","name":"table-cell","data":{"colSpan":1,"rowSpan":1},"nodes":[{"type":"block","id":"1n7l-1705386953337","name":"paragraph","data":{},"nodes":[{"type":"text","id":"o7qu-1705386953336","leaves":[{"text":"提示错误可以设置语言","marks":[]}]}]}]},{"type":"block","id":"wJkR-1705386953341","name":"table-cell","data":{"colSpan":1,"rowSpan":1},"nodes":[{"type":"block","id":"VYI4-1705386953340","name":"paragraph","data":{},"nodes":[{"type":"text","id":"qVDQ-1705386953339","leaves":[{"text":"","marks":[]}]}]}]}]}]},{"type":"block","id":"cfJH-1705386953345","name":"paragraph","data":{},"nodes":[{"type":"text","id":"m7bv-1705386953344","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"Qx7h-1705386953347","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"oOgX-1705386953346","leaves":[{"text":"设置超时","marks":[]}]}]},{"type":"block","id":"0aot-1705386953349","name":"paragraph","data":{},"nodes":[{"type":"text","id":"jF4P-1705386953348","leaves":[{"text":"设置超时时间","marks":[]}]}]},{"type":"block","id":"V10W-1705386953360","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"yc6I-1705386953351","name":"code-line","data":{},"nodes":[{"type":"text","id":"FNnA-1705386953350","leaves":[{"text":"//Sql超时","marks":[]}]}]},{"type":"block","id":"xmCs-1705386953353","name":"code-line","data":{},"nodes":[{"type":"text","id":"WUMk-1705386953352","leaves":[{"text":"db.Ado.CommandTimeOut = 30;//单位秒","marks":[]}]}]},{"type":"block","id":"SKDk-1705386953355","name":"code-line","data":{},"nodes":[{"type":"text","id":"lumb-1705386953354","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"szUY-1705386953357","name":"code-line","data":{},"nodes":[{"type":"text","id":"kJn4-1705386953356","leaves":[{"text":"//db.Open 连接超时","marks":[]}]}]},{"type":"block","id":"aD13-1705386953359","name":"code-line","data":{},"nodes":[{"type":"text","id":"7yQ6-1705386953358","leaves":[{"text":"//在连接池符串加上 Connection Timeout=10  ,默认是30秒,单位秒","marks":[]}]}]}]},{"type":"block","id":"y1NN-1705386953362","name":"paragraph","data":{},"nodes":[{"type":"text","id":"XuyU-1705386953361","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"7zmr-1705386953364","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Mkh7-1705386953363","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"26tr-1705386953366","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"orcw-1705386953365","leaves":[{"text":"验证连接是否成功(数据库连接成功)","marks":[]}]}]},{"type":"block","id":"x4gI-1705386953368","name":"paragraph","data":{},"nodes":[{"type":"text","id":"pKdZ-1705386953367","leaves":[{"text":"版本要求:","marks":[{"type":"color","value":"#ff0000"}]},{"text":"5.0.6.5 preview05 +","marks":[]}]}]},{"type":"block","id":"dkpV-1705386953381","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"RP7G-1705386953370","name":"code-line","data":{},"nodes":[{"type":"text","id":"73RV-1705386953369","leaves":[{"text":"db.Ado.IsValidConnection()  //如果时间长,可以在连接字符串配置 连接超时时间","marks":[]}]}]},{"type":"block","id":"UyMJ-1705386953372","name":"code-line","data":{},"nodes":[{"type":"text","id":"8dQn-1705386953371","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"vjS4-1705386953374","name":"code-line","data":{},"nodes":[{"type":"text","id":"DkyD-1705386953373","leaves":[{"text":"//上面写法如果写到事务中会影响事务","marks":[]}]}]},{"type":"block","id":"yKYz-1705386953376","name":"code-line","data":{},"nodes":[{"type":"text","id":"wtfW-1705386953375","leaves":[{"text":"//事务中我会可以new一个新对象处理","marks":[]}]}]},{"type":"block","id":"ghJl-1705386953378","name":"code-line","data":{},"nodes":[{"type":"text","id":"eouv-1705386953377","leaves":[{"text":" db.CopyNew().Ado.IsValidConnection()","marks":[]}]}]},{"type":"block","id":"PCe0-1705386953380","name":"code-line","data":{},"nodes":[{"type":"text","id":"TeRo-1705386953379","leaves":[{"text":" // 数据库连接成功","marks":[]}]}]}]},{"type":"block","id":"1vhy-1705386953383","name":"paragraph","data":{},"nodes":[{"type":"text","id":"9z4i-1705386953382","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"CwC2-1705386953385","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"Wczk-1705386953384","leaves":[{"text":"打印SQL ","marks":[]}]}]},{"type":"block","id":"N205-1705386953387","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"K96l-1705386953386","leaves":[{"text":"单库sql打印","marks":[]}]}]},{"type":"block","id":"V5fo-1705386953432","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"2UjR-1705386953389","name":"code-line","data":{},"nodes":[{"type":"text","id":"ZKfO-1705386953388","leaves":[{"text":" ","marks":[]}]}]},{"type":"block","id":"FHpS-1705386953391","name":"code-line","data":{},"nodes":[{"type":"text","id":"AE8t-1705386953390","leaves":[{"text":"  SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){","marks":[]}]}]},{"type":"block","id":"uy09-1705386953393","name":"code-line","data":{},"nodes":[{"type":"text","id":"mTYZ-1705386953392","leaves":[{"text":"           ConnectionString = \"连接符字串\", ","marks":[]}]}]},{"type":"block","id":"MVQm-1705386953395","name":"code-line","data":{},"nodes":[{"type":"text","id":"4vqK-1705386953394","leaves":[{"text":"           DbType = DbType.SqlServer,","marks":[]}]}]},{"type":"block","id":"odC2-1705386953397","name":"code-line","data":{},"nodes":[{"type":"text","id":"iQcd-1705386953396","leaves":[{"text":"           IsAutoCloseConnection = true},","marks":[]}]}]},{"type":"block","id":"Uvy5-1705386953399","name":"code-line","data":{},"nodes":[{"type":"text","id":"J9CL-1705386953398","leaves":[{"text":"           db=>{","marks":[]}]}]},{"type":"block","id":"MQzx-1705386953401","name":"code-line","data":{},"nodes":[{"type":"text","id":"WTuu-1705386953400","leaves":[{"text":"                 //5.1.3.24统一了语法和SqlSugarScope一样,老版本AOP可以写外面","marks":[]}]}]},{"type":"block","id":"zPiv-1705386953403","name":"code-line","data":{},"nodes":[{"type":"text","id":"Y3LE-1705386953402","leaves":[{"text":"                 db.Aop.OnLogExecuting = (sql, pars) =>","marks":[]}]}]},{"type":"block","id":"xfic-1705386953405","name":"code-line","data":{},"nodes":[{"type":"text","id":"k8K3-1705386953404","leaves":[{"text":"                 {","marks":[]}]}]},{"type":"block","id":"ritD-1705386953407","name":"code-line","data":{},"nodes":[{"type":"text","id":"ickL-1705386953406","leaves":[{"text":"                     Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响","marks":[]}]}]},{"type":"block","id":"HTZc-1705386953409","name":"code-line","data":{},"nodes":[{"type":"text","id":"Zm4W-1705386953408","leaves":[{"text":"                           ","marks":[]}]}]},{"type":"block","id":"cOQX-1705386953411","name":"code-line","data":{},"nodes":[{"type":"text","id":"smOe-1705386953410","leaves":[{"text":"                     //获取原生SQL推荐 5.1.4.63  性能OK","marks":[]}]}]},{"type":"block","id":"0AzA-1705386953413","name":"code-line","data":{},"nodes":[{"type":"text","id":"arqx-1705386953412","leaves":[{"text":"                     Console.WriteLine(UtilMethods.GetNativeSql(sql,pars))","marks":[]}]}]},{"type":"block","id":"9EIu-1705386953415","name":"code-line","data":{},"nodes":[{"type":"text","id":"9QkQ-1705386953414","leaves":[{"text":"                               ","marks":[]}]}]},{"type":"block","id":"XO5p-1705386953417","name":"code-line","data":{},"nodes":[{"type":"text","id":"8Ybb-1705386953416","leaves":[{"text":"                     //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用","marks":[]}]}]},{"type":"block","id":"uBYf-1705386953419","name":"code-line","data":{},"nodes":[{"type":"text","id":"qmpb-1705386953418","leaves":[{"text":"                     Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))","marks":[]}]}]},{"type":"block","id":"DoPd-1705386953421","name":"code-line","data":{},"nodes":[{"type":"text","id":"wdFP-1705386953420","leaves":[{"text":"                 }","marks":[]}]}]},{"type":"block","id":"31Bi-1705386953423","name":"code-line","data":{},"nodes":[{"type":"text","id":"e0vw-1705386953422","leaves":[{"text":"                 //注意多租户 有几个设置几个","marks":[]}]}]},{"type":"block","id":"jiNC-1705386953425","name":"code-line","data":{},"nodes":[{"type":"text","id":"VxtX-1705386953424","leaves":[{"text":"                 //db.GetConnection(i).Aop","marks":[]}]}]},{"type":"block","id":"7NGR-1705386953427","name":"code-line","data":{},"nodes":[{"type":"text","id":"xNw4-1705386953426","leaves":[{"text":"      });","marks":[]}]}]},{"type":"block","id":"abXz-1705386953429","name":"code-line","data":{},"nodes":[{"type":"text","id":"3l1O-1705386953428","leaves":[{"text":"   //老版本","marks":[]}]}]},{"type":"block","id":"fcoN-1705386953431","name":"code-line","data":{},"nodes":[{"type":"text","id":"CYY1-1705386953430","leaves":[{"text":"   //版本太老没有db=>{}委托写在下面一行就行了","marks":[]}]}]}]},{"type":"block","id":"tBUU-1705386953434","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"jWVE-1705386953433","leaves":[{"text":"多库设置SQL","marks":[]}]}]},{"type":"block","id":"t1iP-1705386953436","name":"paragraph","data":{},"nodes":[{"type":"text","id":"pOOi-1705386953435","leaves":[{"text":"打印和上面有区别,需要一个一个设置","marks":[]}]}]},{"type":"block","id":"b1cD-1705386953480","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"us9d-1705386953438","name":"code-line","data":{},"nodes":[{"type":"text","id":"DGtg-1705386953437","leaves":[{"text":"//注意: ","marks":[]}]}]},{"type":"block","id":"xj7H-1705386953440","name":"code-line","data":{},"nodes":[{"type":"text","id":"gK7d-1705386953439","leaves":[{"text":"//如果你用的 GetConnectionScope或者 GetConnectionScopeWithAttr AOP也应该用 GetConnectionScope ","marks":[]}]}]},{"type":"block","id":"eK1O-1705386953442","name":"code-line","data":{},"nodes":[{"type":"text","id":"f8wT-1705386953441","leaves":[{"text":"//如果你用的 GetConnection或者 GetConnectionWithAttr AOP也应该用 GetConnectionScope ","marks":[]}]}]},{"type":"block","id":"2sob-1705386953444","name":"code-line","data":{},"nodes":[{"type":"text","id":"Tu7Y-1705386953443","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"qYAs-1705386953446","name":"code-line","data":{},"nodes":[{"type":"text","id":"aNzt-1705386953445","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"dgx3-1705386953448","name":"code-line","data":{},"nodes":[{"type":"text","id":"CCUe-1705386953447","leaves":[{"text":"  SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){","marks":[]}]}]},{"type":"block","id":"1u0u-1705386953450","name":"code-line","data":{},"nodes":[{"type":"text","id":"jFY7-1705386953449","leaves":[{"text":"           ConnectionString = \"连接符字串\", ","marks":[]}]}]},{"type":"block","id":"4XXZ-1705386953452","name":"code-line","data":{},"nodes":[{"type":"text","id":"fzpj-1705386953451","leaves":[{"text":"           DbType = DbType.SqlServer,","marks":[]}]}]},{"type":"block","id":"eFWl-1705386953454","name":"code-line","data":{},"nodes":[{"type":"text","id":"XDYN-1705386953453","leaves":[{"text":"           IsAutoCloseConnection = true},","marks":[]}]}]},{"type":"block","id":"ZVQt-1705386953456","name":"code-line","data":{},"nodes":[{"type":"text","id":"RnOL-1705386953455","leaves":[{"text":"           db=>{  ","marks":[]}]}]},{"type":"block","id":"hlxA-1705386953458","name":"code-line","data":{},"nodes":[{"type":"text","id":"VCyH-1705386953457","leaves":[{"text":"                    //也可以这里面循环","marks":[]}]}]},{"type":"block","id":"M1vO-1705386953460","name":"code-line","data":{},"nodes":[{"type":"text","id":"FS9A-1705386953459","leaves":[{"text":"                   db.GetConnection(\"1\").Aop.OnLogExecuting = (sql, pars) =>","marks":[]}]}]},{"type":"block","id":"wXYD-1705386953462","name":"code-line","data":{},"nodes":[{"type":"text","id":"lSZx-1705386953461","leaves":[{"text":"                   {","marks":[]}]}]},{"type":"block","id":"lfFT-1705386953464","name":"code-line","data":{},"nodes":[{"type":"text","id":"hath-1705386953463","leaves":[{"text":"                     Console.WriteLine(\"执行1库\"+sql);","marks":[]}]}]},{"type":"block","id":"OZjs-1705386953466","name":"code-line","data":{},"nodes":[{"type":"text","id":"gVbX-1705386953465","leaves":[{"text":"                   };","marks":[]}]}]},{"type":"block","id":"E4JA-1705386953468","name":"code-line","data":{},"nodes":[{"type":"text","id":"SC1e-1705386953467","leaves":[{"text":"                   db.GetConnection(\"0\").Aop.OnLogExecuting = (sql, pars) =>","marks":[]}]}]},{"type":"block","id":"0XrS-1705386953470","name":"code-line","data":{},"nodes":[{"type":"text","id":"p3Lv-1705386953469","leaves":[{"text":"                   {","marks":[]}]}]},{"type":"block","id":"fSud-1705386953473","name":"code-line","data":{},"nodes":[{"type":"text","id":"ZYw9-1705386953472","leaves":[{"text":"                      Console.WriteLine(\"执行0库\"+sql);","marks":[]}]}]},{"type":"block","id":"hW1c-1705386953475","name":"code-line","data":{},"nodes":[{"type":"text","id":"IbEw-1705386953474","leaves":[{"text":"                   };","marks":[]}]}]},{"type":"block","id":"Su64-1705386953477","name":"code-line","data":{},"nodes":[{"type":"text","id":"UHQt-1705386953476","leaves":[{"text":" ","marks":[]}]}]},{"type":"block","id":"m0M4-1705386953479","name":"code-line","data":{},"nodes":[{"type":"text","id":"akdL-1705386953478","leaves":[{"text":"  });","marks":[]}]}]}]},{"type":"block","id":"GhPS-1705386953482","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Zv2p-1705386953481","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"gOaM-1705386953484","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"bJ9C-1705386953483","leaves":[{"text":"国际化","marks":[]}]}]},{"type":"block","id":"CTrK-1705386953486","name":"paragraph","data":{},"nodes":[{"type":"text","id":"hUVb-1705386953485","leaves":[{"text":"错误提示可以设置相应的语言","marks":[]}]}]},{"type":"block","id":"xtt8-1705386953488","name":"paragraph","data":{},"nodes":[{"type":"text","id":"2q3N-1705386953487","leaves":[{"text":"1.枚举说明","marks":[]}]}]},{"type":"block","id":"yCjf-1705386953501","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"wS41-1705386953490","name":"code-line","data":{},"nodes":[{"type":"text","id":"71AQ-1705386953489","leaves":[{"text":"    public enum LanguageType","marks":[]}]}]},{"type":"block","id":"c3bv-1705386953492","name":"code-line","data":{},"nodes":[{"type":"text","id":"fpfi-1705386953491","leaves":[{"text":"    {","marks":[]}]}]},{"type":"block","id":"40m3-1705386953494","name":"code-line","data":{},"nodes":[{"type":"text","id":"F9wM-1705386953493","leaves":[{"text":"        Default=0, //中&英","marks":[]}]}]},{"type":"block","id":"b9Ip-1705386953496","name":"code-line","data":{},"nodes":[{"type":"text","id":"Hqh8-1705386953495","leaves":[{"text":"        Chinese=1, //处理过的异常尽量中文,未处理的还是英文","marks":[]}]}]},{"type":"block","id":"ndL4-1705386953498","name":"code-line","data":{},"nodes":[{"type":"text","id":"GwlJ-1705386953497","leaves":[{"text":"        English=2 //全部英文","marks":[]}]}]},{"type":"block","id":"mBQU-1705386953500","name":"code-line","data":{},"nodes":[{"type":"text","id":"OYRc-1705386953499","leaves":[{"text":"    }","marks":[]}]}]}]},{"type":"block","id":"ycfr-1705386953503","name":"paragraph","data":{},"nodes":[{"type":"text","id":"bYFF-1705386953502","leaves":[{"text":"2.用例 ","marks":[]}]}]},{"type":"block","id":"beHq-1705386953518","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"fB58-1705386953505","name":"code-line","data":{},"nodes":[{"type":"text","id":"knLE-1705386953504","leaves":[{"text":"  SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()","marks":[]}]}]},{"type":"block","id":"9wny-1705386953507","name":"code-line","data":{},"nodes":[{"type":"text","id":"akNC-1705386953506","leaves":[{"text":"  {","marks":[]}]}]},{"type":"block","id":"wvcJ-1705386953509","name":"code-line","data":{},"nodes":[{"type":"text","id":"63Um-1705386953508","leaves":[{"text":"                ConnectionString = Config.ConnectionString,","marks":[]}]}]},{"type":"block","id":"B57m-1705386953511","name":"code-line","data":{},"nodes":[{"type":"text","id":"QowU-1705386953510","leaves":[{"text":"                DbType = DbType.SqlServer,","marks":[]}]}]},{"type":"block","id":"eYLL-1705386953513","name":"code-line","data":{},"nodes":[{"type":"text","id":"RSsX-1705386953512","leaves":[{"text":"                LanguageType=LanguageType.English,//只显示英文","marks":[]}]}]},{"type":"block","id":"VgH0-1705386953515","name":"code-line","data":{},"nodes":[{"type":"text","id":"VqYY-1705386953514","leaves":[{"text":"                IsAutoCloseConnection = true ","marks":[]}]}]},{"type":"block","id":"V7OY-1705386953517","name":"code-line","data":{},"nodes":[{"type":"text","id":"POjd-1705386953516","leaves":[{"text":"   });","marks":[]}]}]}]},{"type":"block","id":"J62n-1705386953520","name":"paragraph","data":{},"nodes":[{"type":"text","id":"TABs-1705386953519","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"lxYy-1705386953522","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"djSc-1705386953521","leaves":[{"text":"长连接用法(3种方式)","marks":[]}]}]},{"type":"block","id":"Q66x-1705386953524","name":"paragraph","data":{},"nodes":[{"type":"text","id":"LQh3-1705386953523","leaves":[{"text":"1.事务中是长连接","marks":[]}]}]},{"type":"block","id":"nd55-1705386953526","name":"paragraph","data":{},"nodes":[{"type":"text","id":"z5cw-1705386953525","leaves":[{"text":"2.手动释放模式是长连接(不推荐需要手动释放)","marks":[]}]}]},{"type":"block","id":"sPUR-1705386953528","name":"paragraph","data":{},"nodes":[{"type":"text","id":"SSGg-1705386953527","leaves":[{"text":"3.自动释放中 不使用事务我们可以用OpenAlways实现长连接","marks":[]}]}]},{"type":"block","id":"74fW-1705386953545","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"6m5e-1705386953530","name":"code-line","data":{},"nodes":[{"type":"text","id":"PV0h-1705386953529","leaves":[{"text":" //5.0.6.3","marks":[]}]}]},{"type":"block","id":"wNFx-1705386953532","name":"code-line","data":{},"nodes":[{"type":"text","id":"wQ1I-1705386953531","leaves":[{"text":" using (db.Ado.OpenAlways()) {","marks":[]}]}]},{"type":"block","id":"WBhQ-1705386953534","name":"code-line","data":{},"nodes":[{"type":"text","id":"39F6-1705386953533","leaves":[{"text":"    ","marks":[]}]}]},{"type":"block","id":"94za-1705386953536","name":"code-line","data":{},"nodes":[{"type":"text","id":"GvSi-1705386953535","leaves":[{"text":"     db.Queryable...","marks":[]}]}]},{"type":"block","id":"xNlM-1705386953538","name":"code-line","data":{},"nodes":[{"type":"text","id":"bAV1-1705386953537","leaves":[{"text":"     db.Insertable...","marks":[]}]}]},{"type":"block","id":"obid-1705386953540","name":"code-line","data":{},"nodes":[{"type":"text","id":"rncq-1705386953539","leaves":[{"text":"     //比如当前会话生效的临时表就需要长连接,不然创建了访问不了","marks":[]}]}]},{"type":"block","id":"fixr-1705386953542","name":"code-line","data":{},"nodes":[{"type":"text","id":"z9A7-1705386953541","leaves":[{"text":"            ","marks":[]}]}]},{"type":"block","id":"j6Xp-1705386953544","name":"code-line","data":{},"nodes":[{"type":"text","id":"afPf-1705386953543","leaves":[{"text":" }","marks":[]}]}]}]},{"type":"block","id":"rf1H-1705386953547","name":"paragraph","data":{},"nodes":[{"type":"text","id":"lIYF-1705386953546","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"qWcJ-1705386953549","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"gZgq-1705386953548","leaves":[{"text":"配置最小时间","marks":[]}]}]},{"type":"block","id":"6MhI-1705386953551","name":"paragraph","data":{},"nodes":[{"type":"text","id":"CMDi-1705386953550","leaves":[{"text":"新功能:5.0.8.1","marks":[{"type":"color","value":"#ff0000"}]}]}]},{"type":"block","id":"Cp3n-1705386953553","name":"paragraph","data":{},"nodes":[{"type":"text","id":"LpyB-1705386953552","leaves":[{"text":"推荐用默认的,这样多种数据库使用不报错,当然你也可以强制设置","marks":[]}]}]},{"type":"block","id":"2iOy-1705386953562","name":"code","data":{"language":"","theme":"default"},"nodes":[{"type":"block","id":"66hf-1705386953555","name":"code-line","data":{},"nodes":[{"type":"text","id":"06Ca-1705386953554","leaves":[{"text":" db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings","marks":[]}]}]},{"type":"block","id":"TbYE-1705386953557","name":"code-line","data":{},"nodes":[{"type":"text","id":"uv1Y-1705386953556","leaves":[{"text":"  {","marks":[]}]}]},{"type":"block","id":"tuxh-1705386953559","name":"code-line","data":{},"nodes":[{"type":"text","id":"nlMo-1705386953558","leaves":[{"text":"        DbMinDate = DateTime.MinValue//默认最小时间是 1900-01-01 00:00:00.000 ","marks":[]}]}]},{"type":"block","id":"6SNU-1705386953561","name":"code-line","data":{},"nodes":[{"type":"text","id":"I3ZW-1705386953560","leaves":[{"text":"   };","marks":[]}]}]}]},{"type":"block","id":"SqWr-1705386953564","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Mzm9-1705386953563","leaves":[{"text":"                    ","marks":[]}]}]}]">SqlSugar入门

创建对象

你可以使用 SqlSugarClient (new模式)或者 SqlSugarScope (单例) 对数据库进行增、删、查、改等功能注意:除了名字和使用模式不同,功能和API都一模一样
SqlSugarClient(new模式)优点:性能比SqlSugarScope有5%左右提升缺点: db不能跨上下文使用,需要new出一个新对象(EF和Dapper一样),偶发错误难排查适合有经验用户 模式:不能单例,因为他不是线程安全对象
SqlSugarScope (单例模式)5.0.3.4特色功能 如果没有.NET 开发经验的推荐使用 SqlSugarScope能够解决大部分线程安全问题。 原理是用的AsyncLocal异步上下文的不同来实现的线程安全。 注意:同一个异步上下文进行并发操作AsyncLocal也不能保证线程安, 下面3种情况 1.第三方Job  (必须将db改成db.CopyNew()) 2.Task.WhenAll  、ForAsync 、ParallelAsync (必须将db改成db.CopyNew()) 3. 异步漏写await 或者 方法不是Task类型或者Func(Void(方法) Action(表达式)(推荐修改错误代码 , 如果就是不想修改也是可以db.CopyNew()解决)  模式: 单例模式   SqlSugarScope一直new会内存泄露 一定要用单例 
 1、Scope模式:SqlSugarClient  

注意非单例模式用的是SqlSugarClient , 禁止用SqlSugarScope1.1 完整用例

SqlSugarClient 每次请求new一个新对象,db禁止跨上下文使用,IOC建议用Scope或者瞬发注入
  1.     //查询所有
  2.     public static void Demo()
  3.     {
  4.    
  5.      //创建数据库对象 (用法和EF Dappper一样通过new保证线程安全)
  6.      SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){
  7.            ConnectionString = "连接符字串",
  8.            DbType = DbType.SqlServer,
  9.            IsAutoCloseConnection = true},
  10.            db=>{
  11.                  //5.1.3.24统一了语法和SqlSugarScope一样,老版本AOP可以写外面
  12.                  
  13.                   db.Aop.OnLogExecuting = (sql, pars) =>
  14.                   {
  15.                     Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
  16.                     
  17.                     
  18.                     //获取原生SQL推荐 5.1.4.63  性能OK
  19.                      Console.WriteLine(UtilMethods.GetNativeSql(sql,pars))
  20.                               
  21.                     //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用
  22.                     //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
  23.                      
  24.                     
  25.                    };
  26.                   
  27.                    //注意多租户 有几个设置几个
  28.                    //db.GetConnection(i).Aop
  29.            
  30.            });
  31.      
  32.      //建表
  33.      //db.CodeFirst.InitTables<Student>(); 更多看文档迁移        
  34.            
  35.      //查询表的所有
  36.      var list = db.Queryable<Student>().ToList();
  37.      
  38.      //插入
  39.      db.Insertable(new Student() { SchoolId = 1, Name = "jack" }).ExecuteCommand();
  40.      
  41.      //更新
  42.      db.Updateable(new Student() { Id = 1, SchoolId = 2, Name = "jack2" }).ExecuteCommand();
  43.      
  44.      //删除
  45.      db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();
  46.      }
  47.       
  48.     //实体与数据库结构一样
  49.     public class Student
  50.     {
  51.      //数据是自增需要加上IsIdentity
  52.      //数据库是主键需要加上IsPrimaryKey
  53.      //注意:要完全和数据库一致2个属性
  54.      [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
  55.      public int Id { get; set; }
  56.      public int? SchoolId { get; set; }
  57.      public string Name { get; set; }
  58.      }
复制代码
1.2 偶发错误

 SqlSugarClient 出现偶发错误全部换成SqlSugarScope进行测试,如果换了不报错了那么证明就是线程安全问题,你也可以直接使用SqlSugarScope因为他是线程安全的 ,用SqlSugarScope要改成单例不然会有内存增加风险详情:https://www.donet5.com/Home/Doc?typeId=1224 1.3  AddScope IOC

Scope我们需要用SqlSugarClient
  1. //注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行
  2. services.AddHttpContextAccessor();
  3. //注册SqlSugar用AddScoped
  4. services.AddScoped<ISqlSugarClient>(s =>
  5. {
  6.         //Scoped用SqlSugarClient
  7.     SqlSugarClient sqlSugar = new SqlSugarClient (new ConnectionConfig()
  8.     {
  9.         DbType = SqlSugar.DbType.Sqlite,
  10.         ConnectionString = "DataSource=sqlsugar-dev.db",
  11.         IsAutoCloseConnection = true,
  12.     },
  13.    db =>
  14.    {
  15.       //每次上下文都会执行
  16.            
  17.       //获取IOC对象不要求在一个上下文
  18.       //vra log=s.GetService<Log>()
  19.                
  20.       //获取IOC对象要求在一个上下文
  21.       //var appServive = s.GetService<IHttpContextAccessor>();
  22.       //var log= appServive?.HttpContext?.RequestServices.GetService<Log>();
  23.                
  24.        db.Aop.OnLogExecuting = (sql, pars) =>
  25.        {
  26.       
  27.        };
  28.    });
  29.     return sqlSugar;
  30. });
  31. //用接口接收
  32. public class(ISqlSugarClient  db)
复制代码
 2、单例模式:SqlSugarScope 

2.1 完整用例

SqlSugarScope 请使用单例模式,配置参数有2种周期(A):全局生效(B):   当前上下文生效
  1. public  class  SqlSugarHelper //不能是泛型类
  2. {
  3.     //多库情况下使用说明:
  4.     //如果是固定多库可以传 new SqlSugarScope(List<ConnectionConfig>,db=>{}) 文档:多租户
  5.     //如果是不固定多库 可以看文档Saas分库
  6.      
  7.      
  8.    //用单例模式
  9.     public static SqlSugarScope Db= new SqlSugarScope(new ConnectionConfig()
  10.    {
  11.             ConnectionString = "Server=.xxxxx",//连接符字串
  12.             DbType = DbType.SqlServer,//数据库类型
  13.             IsAutoCloseConnection = true //不设成true要手动close
  14.    },
  15.   db=> {
  16.          //(A)全局生效配置点,一般AOP和程序启动的配置扔这里面 ,所有上下文生效
  17.          //调试SQL事件,可以删掉
  18.          db.Aop.OnLogExecuting = (sql, pars) =>
  19.          {
  20.           Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
  21.          
  22.            //获取原生SQL推荐 5.1.4.63  性能OK
  23.            //UtilMethods.GetNativeSql(sql,pars)
  24.                               
  25.            //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用
  26.            //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
  27.                     
  28.          };
  29.          
  30.          //多个配置就写下面
  31.          //db.Ado.IsDisableMasterSlaveSeparation=true;
  32.          
  33.           //注意多租户 有几个设置几个
  34.           //db.GetConnection(i).Aop
  35.      });
  36.    }
  37.    //(B)当前上下文生效配置点,一般多租户相关操作写在这儿
  38.    //db.GetConnectionxxx 只在当前上下文有效,不会共享
  39.    //如果是程序启动时的配置全部到A里面去
  40.   
  41.    //建表
  42.   //db.CodeFirst.InitTables<Student>(); 更多看文档迁移   
  43.   
  44.   //查询表的所有
  45.   var list=SqlSugarHelper.Db.Queryable<Student>().ToList();
  46.      
  47.   //插入
  48.   SqlSugarHelper.Db.Insertable(new Student(){ SchoolId = 1, Name = "jack" }).ExecuteCommand();
  49.      
  50.   //更新
  51.   SqlSugarHelper.Db.Updateable(new Student(){ Id = 1,SchoolId=2,Name="jack2" }).ExecuteCommand();
  52.      
  53.   //删除
  54.   SqlSugarHelper.Db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();
  55.   
  56.   
  57.    //实体与数据库结构一样
  58.    public class Student
  59.    {
  60.      //数据是自增需要加上IsIdentity
  61.      //数据库是主键需要加上IsPrimaryKey
  62.      //注意:要完全和数据库一致2个属性
  63.      [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
  64.      public int Id { get; set; }
  65.      public int? SchoolId { get; set; }
  66.      public string Name { get; set; }
  67.    }
复制代码
验证单例是否成功:SqlSugarHelper.Db.HasCode();//只要看这个hascode在服务启动后都一个那么说明成功了 //IOC直接复制我的DEMO就不需要验证2.2 偶发错误 

详情:https://www.donet5.com/Home/Doc?typeId=12242.3 原理

https://www.donet5.com/Home/Doc?typeId=23522.4  AddSingleton IOC
  1. //注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行
  2. services.AddHttpContextAccessor();
  3. //注册SqlSugar
  4. services.AddSingleton<ISqlSugarClient>(s =>
  5. {
  6.     SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
  7.     {
  8.         DbType = SqlSugar.DbType.Sqlite,
  9.         ConnectionString = "DataSource=sqlsugar-dev.db",
  10.         IsAutoCloseConnection = true,
  11.     },
  12.    db =>
  13.    {         
  14.       //每次上下文都会执行
  15.            
  16.       //获取IOC对象不要求在一个上下文
  17.       //vra log=s.GetService<Log>()
  18.                
  19.       //获取IOC对象要求在一个上下文
  20.       //var appServive = s.GetService<IHttpContextAccessor>();
  21.       //var log= appServive?.HttpContext?.RequestServices.GetService<Log>();
  22.                
  23.        db.Aop.OnLogExecuting = (sql, pars) =>
  24.        {
  25.       
  26.        };
  27.    });
  28.     return sqlSugar;
  29. });
  30. //用接口接收
  31. public class(ISqlSugarClient  db)
复制代码
 多库用法 
  1. var db = new SqlSugarClient(new List<ConnectionConfig>()
  2. {
  3. new ConnectionConfig(){ConfigId="0",DbType=DbType.SqlServer,ConnectionString=..,IsAutoCloseConnection=true},
  4. new ConnectionConfig(){ConfigId="1",DbType=DbType.MySql,ConnectionString=..,IsAutoCloseConnection=true  }
  5. });
  6. var childA=db.GetConnection("A");
  7. var childB=db.GetConnection("B");
复制代码
详细教程:https://www.donet5.com/Home/Doc?typeId=2246 连接参数SqlSugarClient是通过ConnectionConfig进行传参数详细参数如下
名称描述必填
DbType数据库类型
ConnectionString连接字符串
IsAutoCloseConnection自动释放和关闭数据库连接,如果有事务事务结束时关闭,否则每次操作后关闭 
ConfigureExternalServices一些扩展层务的集成 
MoreSettings

  • 点击查看
 
SlaveConnectionConfigs主从设置 
LanguageType提示错误可以设置语言 
 设置超时设置超时时间
  1. //Sql超时
  2. db.Ado.CommandTimeOut = 30;//单位秒
  3. //db.Open 连接超时
  4. //在连接池符串加上 Connection Timeout=10  ,默认是30秒,单位秒
复制代码
  验证连接是否成功(数据库连接成功)版本要求:5.0.6.5 preview05 +
  1. db.Ado.IsValidConnection()  //如果时间长,可以在连接字符串配置 连接超时时间
  2. //上面写法如果写到事务中会影响事务
  3. //事务中我会可以new一个新对象处理
  4. db.CopyNew().Ado.IsValidConnection()
  5. // 数据库连接成功
复制代码
 打印SQL 

单库sql打印

 
  1.   SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){
  2.            ConnectionString = "连接符字串",
  3.            DbType = DbType.SqlServer,
  4.            IsAutoCloseConnection = true},
  5.            db=>{
  6.                  //5.1.3.24统一了语法和SqlSugarScope一样,老版本AOP可以写外面
  7.                  db.Aop.OnLogExecuting = (sql, pars) =>
  8.                  {
  9.                      Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
  10.                            
  11.                      //获取原生SQL推荐 5.1.4.63  性能OK
  12.                      Console.WriteLine(UtilMethods.GetNativeSql(sql,pars))
  13.                               
  14.                      //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用
  15.                      Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))
  16.                  }
  17.                  //注意多租户 有几个设置几个
  18.                  //db.GetConnection(i).Aop
  19.       });
  20.    //老版本
  21.    //版本太老没有db=>{}委托写在下面一行就行了
复制代码
多库设置SQL

打印和上面有区别,需要一个一个设置
  1. //注意:
  2. //如果你用的 GetConnectionScope或者 GetConnectionScopeWithAttr AOP也应该用 GetConnectionScope
  3. //如果你用的 GetConnection或者 GetConnectionWithAttr AOP也应该用 GetConnectionScope
  4.   SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){
  5.            ConnectionString = "连接符字串",
  6.            DbType = DbType.SqlServer,
  7.            IsAutoCloseConnection = true},
  8.            db=>{  
  9.                     //也可以这里面循环
  10.                    db.GetConnection("1").Aop.OnLogExecuting = (sql, pars) =>
  11.                    {
  12.                      Console.WriteLine("执行1库"+sql);
  13.                    };
  14.                    db.GetConnection("0").Aop.OnLogExecuting = (sql, pars) =>
  15.                    {
  16.                       Console.WriteLine("执行0库"+sql);
  17.                    };
  18.   });
复制代码
 国际化

错误提示可以设置相应的语言1.枚举说明
  1.     public enum LanguageType
  2.     {
  3.         Default=0, //中&英
  4.         Chinese=1, //处理过的异常尽量中文,未处理的还是英文
  5.         English=2 //全部英文
  6.     }
复制代码
2.用例 
  1.   SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
  2.   {
  3.                 ConnectionString = Config.ConnectionString,
  4.                 DbType = DbType.SqlServer,
  5.                 LanguageType=LanguageType.English,//只显示英文
  6.                 IsAutoCloseConnection = true
  7.    });
复制代码
 长连接用法(3种方式)

1.事务中是长连接

2.手动释放模式是长连接(不推荐需要手动释放)

3.自动释放中 不使用事务我们可以用OpenAlways实现长连接
  1. //5.0.6.3
  2. using (db.Ado.OpenAlways()) {
  3.    
  4.      db.Queryable...
  5.      db.Insertable...
  6.      //比如当前会话生效的临时表就需要长连接,不然创建了访问不了
  7.             
  8. }
复制代码
 配置最小时间

新功能:5.0.8.1推荐用默认的,这样多种数据库使用不报错,当然你也可以强制设置
  1. db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings
  2. {
  3.         DbMinDate = DateTime.MinValue//默认最小时间是 1900-01-01 00:00:00.000
  4. };
复制代码
 
来源:https://www.cnblogs.com/yswenli/p/17967640
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

上一篇: SqlSugar入门

下一篇: SqlSugar入门

举报 回复 使用道具