-
Notifications
You must be signed in to change notification settings - Fork 1.4k
2.2 Dynamic Update
1、Dictionary update (multi-library support)
// Dictionary
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();2、Dictionary collection (multi-library support)
// Dictionary collection
var dtList = new List<Dictionary<string, object>>();
dtList.Add(dt);
dtList.Add(dt2);
var t666 = db.Updateable(dtList).AS("student").WhereColumns("id").ExecuteCommand();3、Anonymous object update This method does not support batch, if batch dictionary mode
db.UpdateableByDynamic
(new { id = 1, name = "a" })
.AS("order")
.WhereColumns("id").ExecuteCommand();
//sql
UPDATE [order] SET
[name]=@name WHERE [id]=@id
@id:1,@name:a4、Dynamic object update
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();5、SQL mode update The where condition is too flexible and there may be incompatibilities between different databases
db.Updateable<object>()
.AS("Order")
.SetColumns("name", 1)
.Where("id=1").ExecuteCommand();6、BulkCopy update (requires a newer version) The condition is updated based on ID
db.Fastest<DataTable>().AS("Order").BulkUpdate(datatable, new string[] { "id" });7、Dynamic class update (the most perfect support for multi-library) The above may be simpler, but this feature is mainly used for products that are more compatible with multiple libraries and can support features such as 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 ()// Cache the KEY, which is based on the table name and field name.
.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(); // Insert or update
db.QueryableByObject(typeof(OrderSpliteTest)).ToList();Expression mode update You need to upgrade to 5.4.130-preview10
// Execute once when the program is active StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser);
// The business code has no physical update
var a = "a";
var b = "c";
db.UpdateableByObject(typeof(StudentWithSnowflake))
.SetColumns("it",
// Hard coding FormattableString str = $"it=>it.Id>1"; // Dynamic conversion FormattableString str2 = FormattableStringFactory.Create("it=>it.Id>{0}",0); More usage: https://www.donet5.com/Home/Doc?typeId=2562