SqlSugar 是一款 老牌 .NET 开源ORM框架,由果糖大数据科技团队维护和更新 ,开箱即用最易上手的ORM框架 。生态圈丰富,目前开源生态仅次于微软的EF Core。文章主要记录一些使用SqlSugar的新增或修改小技巧
 1 新增或修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   |  StudentInfo studentInfo = new StudentInfo() {     Id = 31,     Name = "新增的数据",     CreateTime = DateTime.Now,     Isdeleted = false,     SchoolId = 0, };
 
  db.Storageable(studentInfo).ExecuteCommand(); studentInfo.Name = "数据已存在就修改"; db.Storageable(studentInfo).ExecuteCommand();
 
 
  List<StudentInfo> addlist = new List<StudentInfo>(); for (int i = 0; i < 20; i++) {     addlist.Add(new StudentInfo()     {         Id = i + 1,         CreateTime = DateTime.Now,         Isdeleted = false,         Name = $"名称_{i + 1}",         SchoolId = i + 1,     }); } db.Storageable<StudentInfo>(addlist).ExecuteCommand(); foreach (var item in addlist) {     item.Name = $"批量修改"; } db.Storageable<StudentInfo>(addlist).ExecuteCommand();
 
  | 
 2 主键Id是否为0插入否则更新(不验证数据库是否存在)
只更新修改字段1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   |  StudentInfo obj = new StudentInfo() {     CreateTime = DateTime.Now,     Isdeleted = false,     Name = $"名称_41",     SchoolId = 41, };
  db.Storageable(obj) .SplitUpdate(it => it.Item.Id > 0) .SplitInsert(it => it.Item.Id == 0).ExecuteCommand();
  obj.Id = 1; obj.Name = "修改一下数据"; db.Storageable(obj)   .SplitUpdate(it => it.Item.Id > 0)   .SplitInsert(it => Item.Id == 0).ExecuteCommand();
 
 
  | 
  3 更新忽略字段
1 2 3 4 5 6 7 8
   |  upobj.Name = "Name修改了"; upobj.CreateTime = DateTime.Now.AddYears(10);
 
  StorageableResult<StudentInfo> storageableResult1 = db.Storageable(upobj)    .ToStorage(); storageableResult1.AsUpdateable.IgnoreColumns(z => z.Name).ExecuteCommand();
 
  | 
 4 无主键操作
1 2 3 4 5
   | upobj.CreateTime = DateTime.Now.AddYears(-5); upobj.Name = "无主键操作"; db.Storageable(upobj)   .WhereColumns(it => it.Id)   .ExecuteCommand();
   | 
 5 大数据更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | db.Deleteable<StudentInfo>().ExecuteCommand(); 
  var addlist = new List<StudentInfo>(); for (int i = 0; i < 100000; i++) {     addlist.Add(new StudentInfo()     {         Id = i + 1,         CreateTime = DateTime.Now,         Isdeleted = false,         Name = $"名称_{i + 1}",         SchoolId = i + 1,     }); } StorageableResult<StudentInfo> storageableResult = db.     Storageable<StudentInfo>(addlist).ToStorage(); storageableResult.BulkCopy();
   | 
 6 更新字典数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("Id", 456789); dic.Add("SchoolId", 456789); dic.Add("Name", "字典用法"); dic.Add("CreateTime", DateTime.Now); dic.Add("Isdeleted", 1); dictionaryList.Add(dic);
  DataTableResult tableResult = db.Storageable(dictionaryList, "StudentInfo")     .WhereColumns("id")     .ToStorage();
  tableResult.AsInsertable.ExecuteCommand();
  dictionaryList[0]["Name"] = "修改名称了"; DataTableResult tableResult1 = db.Storageable(dictionaryList, "StudentInfo")  .WhereColumns("id")  .ToStorage(); tableResult1.AsUpdateable.ExecuteCommand();
   | 
 6 分页处理
1 2 3 4 5 6 7 8 9 10 11 12
   | List<StudentInfo> list = db.Queryable<StudentInfo>().ToList();
  db.Utilities.PageEach(list, 2000, pageList => {      db.Storageable(pageList).ExecuteCommand();                         
  });
   |