指定列更新
db.Update<School>(new { name = "蓝翔14" }, it => it.id == 14); //只更新name列 条件 id=14 db.Update<School, int>(new { name = "蓝翔11 23 12", areaId = 2 }, 11, 23, 12);//更新name和areaId 根据主键 db.Update<School, string>(new { name = "蓝翔2" }, new string[] { "11", "21" }); db.Update<School>(new { name = "蓝翔2" }, it => it.id == 100); var array=new int[]{1,2,3}; db.Update<School>(new { name = "蓝翔2" }, it => array.Contains(it.id));// id in 1,2,3
使用字典更新
var dic = new Dictionary<string, string>(); dic.Add("name", "第十三条"); dic.Add("areaId", "1"); db.Update<School, int>(dic, 13);
整个实体更新
db.Update(new School { id = 16, name = "蓝翔16", AreaId = 1 }); db.Update<School>(new School { id = 12, name = "蓝翔12", AreaId = 2 }, it => it.id == 18); db.Update<School>(new School() { id = 11, name = "青鸟11" });
与指定列更新不同的是只要实体中有的字段都会更新,如果想不更新指定字段请用排除更新列
设置不更新列
db.DisableUpdateColumns = new string[] { "CreateTime" };//设置CreateTime不更新TestUpdateColumns updObj = new TestUpdateColumns() { VGUID = Guid.Parse("542b5a27-6984-47c7-a8ee-359e483c8470"), Name = "xx", Name2 = "xx2", IdentityField = 0, CreateTime = null }; //CreateTime将不会被更新 db.Update(updObj); //以前实现这种更新需要用指定列的方式实现,现在就简单多了。 //批量更新 数据量小时建议使用 var updateResult = db.UpdateRange(GetUpdateList()); //批量更新 数据量大时建议使用 var updateResult2 = db.SqlBulkReplace(GetUpdateList2()); //更新字符串 db.Update<Student>("sch_id=sch_id+1", it => it.id == 1); //清空禁止更新列 db.DisableUpdateColumns = null; //新语法添加禁止更新列 db.AddDisableUpdateColumn("id", "name");//添加禁止更新列
只更新非null
特殊用法只能在 .NET 4.+并且是WEB项目中有效(core不支持)
//web里面有效 db.IsIgnoreErrorColumns = true;//过滤表单错误列 var pars= SqlSugar.SqlSugarTool.GetParameterDictionary(true);//将表单里面有值的参数转成字典 db.Update<Student>(pars, it => it.id == 1);//更新
2016 © CodeIsBug.comApache Licence 2.0