mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-01 19:03:58 +08:00
-
This commit is contained in:
52
OrmTest/UnitTest/Insert.cs
Normal file
52
OrmTest/UnitTest/Insert.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using OrmTest.Models;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.UnitTest
|
||||
{
|
||||
public class Insert : ExpTestBase
|
||||
{
|
||||
private Insert() { }
|
||||
public Insert(int eachCount)
|
||||
{
|
||||
this.Count = eachCount;
|
||||
}
|
||||
|
||||
public void Init() {
|
||||
var db = GetInstance();
|
||||
var insertObj = new Student() { Name="jack",CreateTime=DateTime.Now };
|
||||
var insertObjs = new List<Student>() { insertObj }.ToArray();
|
||||
|
||||
//Insert reutrn identity
|
||||
db.Insertable<Student>(insertObj).ExecuteReutrnIdentity();
|
||||
|
||||
//Insert reutrn Command Count
|
||||
db.Insertable<Student>(insertObj).ExecuteCommand();
|
||||
|
||||
//Only insert Name
|
||||
db.Insertable<Student>(insertObj).InsertColumns(it => new object[] { it.Name}).ExecuteReutrnIdentity();
|
||||
|
||||
//Ignore Name and TestId
|
||||
db.Insertable<Student>(insertObj).IgnoreColumns(it => new object[] { it.Name,it.TestId }).ExecuteReutrnIdentity();
|
||||
|
||||
//Use Lock
|
||||
db.Insertable<Student>(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
|
||||
|
||||
//ToSql
|
||||
db.Insertable<Student>(insertObj).With(SqlWith.UpdLock).InsertColumns(it => new object[] { it.Name }).ToSql();
|
||||
|
||||
//Insert List<T>
|
||||
db.Insertable<Student>(insertObjs).With(SqlWith.UpdLock).ExecuteCommand();
|
||||
}
|
||||
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new SystemTablesConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection=true });
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user