打包提交

新版本用法

using var tran = db.UseTran();//using不能少

db.Queryable<Order>().ToList();
db.Insertable(new Order() { Name = "a" }).ExecuteCommand();
 
tran.CommitTran();//commitTran提交,如果没有提交成功会自动回滚

//注意:using不能少不然意外会导致事务无法释放

老版本用法

1. 一些自定义的sql或者存储过程一起打包可能会不支持

 2. 个别数据库参数化有上限,只适合少量操作的增删查改,不适合一次操作太多

 3.自带的批量插入性能很好,不要在这儿一条一条去循环.AddQueue()

打包提交默认带有事务

//将1一个操作带事务提交
var db = GetInstance();
db.Insertable<Student>(new Student() { Name = "a" }).AddQueue();
db.SaveQueues();

//将2个操作带事务提交
var db = GetInstance();
db.Insertable<Student>(new Student() { Name = "a" }).AddQueue();
db.Updateable<Student>(new Student() { Name = "b" }).AddQueue();
db.SaveQueues();  

//异步提交
db.Insertable<Student>(new Student() { Name = "a" }).AddQueue();
db.Updateable<Student>(new Student() { Name = "b" }).AddQueue();
var ar = db.SaveQueuesAsync(); 

//多个查询返回
db.Queryable<Student>().AddQueue();
db.Queryable<School>().AddQueue();
db.AddQueue("select * from student where id=@id", new { id = 1 }); 
var result2 = db.SaveQueues<Student, School, Student>();
//也可以没有返回值db.SaveQueues();


AddQueue失效

(1)SqlSugarClient

db.ContextId是同一个就行了(禁止单例)

(2)SqlSugarScope

情况比较特殊遇到了可以看一下

异步方法中 :当AddQueue和SaveChagneAsync不同在一个方法会出现,因为微软的AsyncLocal存储机质问题决

   (2.1)使用单例模式

   (2.2)在声名sqlsugar调用一下ContextId属性让他第一时间创建DB这样就能共享了

image.png


关闭
果糖网