-
Notifications
You must be signed in to change notification settings - Fork 1.3k
2.2 Dynamic Update
果糖网 edited this page Jul 2, 2024
·
3 revisions
// 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();
// 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();
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: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();
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();
The condition is updated based on ID
db.Fastest<DataTable>().AS("Order").BulkUpdate(datatable, new string[] { "id" });
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();