Skip to content
果糖网 edited this page Jul 2, 2024 · 1 revision

1.Insert Object

//insert entity or List<entity>
db.Insertable(insertObj).ExecuteCommand();

//Insert Dictionary or List<Dictionary >
var dt = new Dictionary<string, object>();
            dt.Add("name", "1");
            dt.Add("CreateTime", null);
db.Insertable(dt).AS("student").ExecuteReturnIdentity();

2.Return Type

Final Execution Method description
ExecuteCommand Return the number of inserted rows
ExecuteReturnIdentity Return identity int
ExecuteReturnBigIdentity Return identity long
ExecuteReturnEntity Return entity
ExecuteCommandIdentityIntoEntity Return the number of inserted rows and update entity identity property

3.Detailed usage

//Ignore Name testId
db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReturnIdentity();
db.Insertable(insertObj).IgnoreColumns( "Name","TestId").ExecuteReturnIdentity();

//Only  insert  Name and SchoolId
db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReturnIdentity();
db.Insertable(insertObj).InsertColumns("Name","SchoolId").ExecuteReturnIdentity();

//ignore null columns
db.Insertable(insertObj2).IgnoreColumnsNull().ExecuteCommand();

//Use Lock
db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();

4.Transcoding storage

For example, MYSQL special symbol

[SugarColumn(IsTranscoding =true)]
public string Url { get; set; }

5.Entity Settings Non-Inserted Columns

[SqlSugar.SugarColumn(IsOnlyIgnoreInsert = true)]
public DateTime CreateTime { get; set; }

6. Async

Db.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();