2017-04-30 22:49:41 +08:00
|
|
|
|
using OrmTest.Models;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace OrmTest.UnitTest
|
|
|
|
|
{
|
2017-05-16 13:55:57 +08:00
|
|
|
|
public class Insert : UnitTestBase
|
2017-04-30 22:49:41 +08:00
|
|
|
|
{
|
|
|
|
|
private Insert() { }
|
|
|
|
|
public Insert(int eachCount)
|
|
|
|
|
{
|
|
|
|
|
this.Count = eachCount;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 21:12:23 +08:00
|
|
|
|
public void Init()
|
|
|
|
|
{
|
2017-04-30 22:49:41 +08:00
|
|
|
|
var db = GetInstance();
|
2017-05-22 02:14:28 +08:00
|
|
|
|
var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId=0 };
|
2017-05-01 14:23:13 +08:00
|
|
|
|
db.IgnoreColumns.Add("TestId", "Student");
|
2017-05-01 23:00:05 +08:00
|
|
|
|
//db.MappingColumns.Add("id","dbid", "Student");
|
2017-05-21 21:12:23 +08:00
|
|
|
|
|
2017-05-21 13:58:35 +08:00
|
|
|
|
var t1 = db.Insertable(insertObj).ToSql();
|
|
|
|
|
base.Check(@"INSERT INTO [Student]
|
|
|
|
|
([SchoolId],[Name],[CreateTime])
|
|
|
|
|
VALUES
|
|
|
|
|
(@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();",
|
|
|
|
|
new List<SugarParameter>() {
|
|
|
|
|
new SugarParameter("@SchoolId",0),
|
|
|
|
|
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
|
|
|
|
new SugarParameter("@Name","jack")
|
|
|
|
|
}, t1.Key, t1.Value, "Insert t1 error"
|
|
|
|
|
);
|
2017-04-30 22:49:41 +08:00
|
|
|
|
|
|
|
|
|
//Insert reutrn Command Count
|
2017-05-21 21:12:23 +08:00
|
|
|
|
var t2 = db.Insertable(insertObj).ExecuteCommand();
|
2017-04-30 22:49:41 +08:00
|
|
|
|
|
2017-05-07 23:26:49 +08:00
|
|
|
|
db.IgnoreColumns = null;
|
2017-04-30 22:49:41 +08:00
|
|
|
|
//Only insert Name
|
2017-05-21 21:12:23 +08:00
|
|
|
|
var t3 = db.Insertable(insertObj).InsertColumns(it => new { it.Name }).ToSql();
|
2017-05-21 13:58:35 +08:00
|
|
|
|
base.Check(@"INSERT INTO [Student]
|
|
|
|
|
([Name])
|
|
|
|
|
VALUES
|
|
|
|
|
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
|
|
|
|
|
new SugarParameter("@Name","jack")
|
|
|
|
|
}, t3.Key, t3.Value, "Insert t3 error");
|
|
|
|
|
|
2017-04-30 22:49:41 +08:00
|
|
|
|
|
|
|
|
|
//Ignore Name and TestId
|
2017-05-21 21:12:23 +08:00
|
|
|
|
var t4 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
|
2017-05-21 14:05:06 +08:00
|
|
|
|
base.Check(@"INSERT INTO [Student]
|
|
|
|
|
([SchoolId],[CreateTime])
|
|
|
|
|
VALUES
|
|
|
|
|
(@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
|
|
|
|
|
new List<SugarParameter>() {
|
|
|
|
|
new SugarParameter("@SchoolId",0),
|
|
|
|
|
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
|
|
|
|
}, t4.Key, t4.Value, "Insert t4 error"
|
|
|
|
|
);
|
2017-04-30 22:49:41 +08:00
|
|
|
|
|
2017-05-07 23:52:34 +08:00
|
|
|
|
//Ignore Name and TestId
|
2017-05-21 14:05:06 +08:00
|
|
|
|
var t5 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
|
|
|
|
|
base.Check(@"INSERT INTO [Student] WITH(UPDLOCK)
|
|
|
|
|
([SchoolId],[CreateTime])
|
|
|
|
|
VALUES
|
|
|
|
|
(@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
|
|
|
|
|
new List<SugarParameter>() {
|
|
|
|
|
new SugarParameter("@SchoolId",0),
|
|
|
|
|
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
|
|
|
|
}, t5.Key, t5.Value, "Insert t5 error"
|
|
|
|
|
);
|
2017-04-30 22:49:41 +08:00
|
|
|
|
//Use Lock
|
2017-05-21 21:12:23 +08:00
|
|
|
|
var t6 = db.Insertable(insertObj).With(SqlWith.UpdLock).ToSql();
|
2017-05-21 14:09:37 +08:00
|
|
|
|
base.Check(@"INSERT INTO [Student] WITH(UPDLOCK)
|
|
|
|
|
([SchoolId],[Name],[CreateTime],[TestId])
|
|
|
|
|
VALUES
|
|
|
|
|
(@SchoolId,@Name,@CreateTime,@TestId) ;SELECT SCOPE_IDENTITY();",
|
|
|
|
|
new List<SugarParameter>() {
|
|
|
|
|
new SugarParameter("@SchoolId",0),
|
|
|
|
|
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
|
|
|
|
|
new SugarParameter("@Name","jack"),
|
|
|
|
|
new SugarParameter("@TestId",0)
|
|
|
|
|
}, t6.Key, t6.Value, "Insert t6 error"
|
|
|
|
|
);
|
2017-05-17 17:10:25 +08:00
|
|
|
|
|
2017-05-22 02:14:28 +08:00
|
|
|
|
var insertObj2 = new Student() { Name = null,SchoolId=0, CreateTime = Convert.ToDateTime("2010-1-1") };
|
2017-05-21 21:12:23 +08:00
|
|
|
|
var t8 = db.Insertable(insertObj2).Where(true/* Is insert null */, true/*off identity*/).ToSql();
|
|
|
|
|
base.Check(@"
|
|
|
|
|
INSERT INTO [Student]
|
|
|
|
|
([SchoolId],[CreateTime],[TestId])
|
|
|
|
|
VALUES
|
|
|
|
|
(@SchoolId,@CreateTime,@TestId) ;SELECT SCOPE_IDENTITY();",
|
|
|
|
|
new List<SugarParameter>() {
|
|
|
|
|
new SugarParameter("@SchoolId", 0),
|
|
|
|
|
new SugarParameter("@CreateTime", Convert.ToDateTime("2010-1-1")),
|
|
|
|
|
new SugarParameter("@TestId", 0)
|
|
|
|
|
},
|
|
|
|
|
t8.Key,
|
|
|
|
|
t8.Value,
|
|
|
|
|
"Insert t8 error"
|
|
|
|
|
);
|
2017-05-17 17:10:25 +08:00
|
|
|
|
|
|
|
|
|
|
2017-05-15 16:45:49 +08:00
|
|
|
|
db.IgnoreColumns = new IgnoreComumnList();
|
|
|
|
|
db.IgnoreColumns.Add("TestId", "Student");
|
2017-05-17 17:10:25 +08:00
|
|
|
|
|
2017-04-30 22:49:41 +08:00
|
|
|
|
//Insert List<T>
|
2017-05-16 13:36:37 +08:00
|
|
|
|
var insertObjs = new List<Student>();
|
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
|
{
|
2017-05-21 21:12:23 +08:00
|
|
|
|
insertObjs.Add(new Student() { Name = "name" + i });
|
2017-05-16 13:36:37 +08:00
|
|
|
|
}
|
2017-05-21 21:12:23 +08:00
|
|
|
|
var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).With(SqlWith.UpdLock).ToSql();
|
2017-04-30 22:49:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SqlSugarClient GetInstance()
|
|
|
|
|
{
|
2017-05-23 06:16:59 +08:00
|
|
|
|
SqlSugarClient db = new SqlSugarClient(new SystemTableConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
2017-04-30 22:49:41 +08:00
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|