//字典 var dt = new Dictionary<string, object>(); dt.Add("id", 1); dt.Add("name", "jack"); dt.Add("createTime", DateTime.Now); var t66 = db.Updateable(dt).AS("student").WhereColumns("id").ExecuteCommand();
//字典集合 var dtList = new List<Dictionary<string, object>>(); dtList.Add(dt); dtList.Add(dt2); var t666 = db.Updateable(dtList).AS("student").WhereColumns("id").ExecuteCommand();
该方法不支持批量,如果批量字典方式
db.UpdateableByDynamic (new { id = 1, name = "a" }) .AS("order") .WhereColumns("id").ExecuteCommand(); //sql UPDATE [order] SET [name]=@name WHERE [id]=@id @id:1,@name:a
ExpandoObject ex = new ExpandoObject(); var dic= (IDictionary<string, object>)(ex); dic.Add("name", "1"); dic.Add("id", SnowFlakeSingle.Instance.NextId()); db.Upateable(new Dictionary<string, object>(ex)).AS("StudentWithSnowflake08").ExecuteCommand();
where条件太灵活可能会有不同数据库不兼容情况
db.Updateable<object>() .AS("Order") .SetColumns("name", 1) .Where("id=1").ExecuteCommand();
根据ID为条件更新
db.Fastest<DataTable>().AS("Order").BulkUpdate(datatable, new string[] { "id" });
上面的可能简单些,不过该功能主要用于产品对多库兼容更好,可以支持AOP等功能
var type = db.DynamicBuilder().CreateClass("table1", new SugarTable() { }) .CreateProperty("Id",typeof(int),new SugarColumn() {IsPrimaryKey=true,IsIdentity=true }) .CreateProperty("Name",typeof(string), new SugarColumn() { }) .WithCache()//缓存起来根据表名和字段名组合的KEY 、 .BuilderType(); db.CodeFirst.InitTables(type); var dic=new Dictionary<string, object>(){{"Id", 1 }, { "Name", "jack"}}; var value=db.DynamicBuilder().CreateObjectByType(type,dic); db.InsertableByObject(value).ExecuteCommand(); db.UpdateableByObject(value).ExecuteCommand(); db.DeleteableByObject(value).ExecuteCommand(); db.StorageableByObject(value).ExecuteCommand();//插入或者更新 //查询 5.1.4.84-preview09+ db.QueryableByObject(typeof(OrderSpliteTest)).ToList();
更多用法:https://www.donet5.com/Home/Doc?typeId=2562
需要升级到:5.4.130-preview10
//程序动时执行一次 StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser); //业务代码无实体 更新 var a = "a"; var b = "c"; db.UpdateableByObject(typeof(StudentWithSnowflake)) .SetColumns("it", $"it.Name== it.Name.Replace({a},{b}) ") .Where("it", $"it.Name.Contains({a})") .ExecuteCommand(); //硬编码 FormattableString str = $"it=>it.Id>1"; //动态转换 FormattableString str2 = FormattableStringFactory.Create("it=>it.Id>{0}",0);
更多用法:https://www.donet5.com/Home/Doc?typeId=2562
2016 © donet5.comApache Licence 2.0