无实体插入

1、字典插入(支持多库)

//可以是 Dictionary 或者 List<Dictionary >
var dc= new Dictionary<string, object>();
            dc.Add("name", "1");
            dc.Add("CreateTime", DateTime.Now);
db.Insertable(dc).AS("student").ExecuteCommand();

2、匿名对象插入(支持多库)

db.InsertableByDynamic(new   { name="",price=1 })
                .AS("OrderInfo")
                .ExecuteCommand();

3、动态对象更新 

Select<Object>().ToList()这种就是动态类型

 ExpandoObject ex = new ExpandoObject();
 var dic=  (IDictionary<string, object>)(ex);
 dic.Add("name", "1");
 dic.Add("id", SnowFlakeSingle.Instance.NextId());
 db.Insertable(new Dictionary<string, object>(ex)).AS("StudentWithSnowflake08").ExecuteCommand();

4、BulkCopy

db.Fastest<System.Data.DataTable>().AS("order").BulkCopy(dataTable);
//具体用法 要看文档  https://www.donet5.com/Home/Doc?typeId=2404

5、动态建类插入 (完美支持多库+AOP)

该功能对多库支持更好,并且可以支持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

关闭
果糖网