SqlSugar/Src/Asp.Net/SqlServerTest/_OldTest/UnitTest/Insert.cs

153 lines
5.7 KiB
C#
Raw Normal View History

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();
2017-05-26 14:31:19 +08:00
base.Check(@"INSERT INTO [STudent]
2017-05-21 13:58:35 +08:00
([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
2017-05-24 13:43:08 +08:00
2017-04-30 22:49:41 +08:00
//Insert reutrn Command Count
2017-09-01 12:54:02 +08:00
var t2 = db.Insertable(insertObj).ExecuteReturnEntity();
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-26 14:31:19 +08:00
base.Check(@"INSERT INTO [STudent]
2017-05-21 13:58:35 +08:00
([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-26 14:31:19 +08:00
base.Check(@"INSERT INTO [STudent]
2017-05-21 14:05:06 +08:00
([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();
2017-05-26 14:31:19 +08:00
base.Check(@"INSERT INTO [STudent] WITH(UPDLOCK)
2017-05-21 14:05:06 +08:00
([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-26 14:31:19 +08:00
base.Check(@"INSERT INTO [STudent] WITH(UPDLOCK)
([SchoolId],[Name],[CreateTime])
2017-05-21 14:09:37 +08:00
VALUES
2017-05-26 14:31:19 +08:00
(@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();",
2017-05-21 14:09:37 +08:00
new List<SugarParameter>() {
new SugarParameter("@SchoolId",0),
new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
2017-05-26 14:31:19 +08:00
new SugarParameter("@Name","jack")
2017-05-21 14:09:37 +08:00
}, 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();
2017-05-26 14:31:19 +08:00
base.Check(@"INSERT INTO [STudent]
2017-06-14 16:30:03 +08:00
([ID],[SchoolId],[CreateTime])
2017-05-21 21:12:23 +08:00
VALUES
2017-06-14 16:30:03 +08:00
(@ID,@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
2017-05-21 21:12:23 +08:00
new List<SugarParameter>() {
new SugarParameter("@SchoolId", 0),
2017-06-14 16:30:03 +08:00
new SugarParameter("@ID", 0),
2017-05-26 14:31:19 +08:00
new SugarParameter("@CreateTime", Convert.ToDateTime("2010-1-1"))
2017-05-21 21:12:23 +08:00
},
t8.Key,
t8.Value,
"Insert t8 error"
);
2017-05-17 17:10:25 +08:00
2017-07-15 20:20:42 +08:00
db.IgnoreColumns = new IgnoreColumnList();
2017-05-15 16:45:49 +08:00
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-05-24 13:43:08 +08:00
insertObj.Name = null;
var t10 = db.Insertable(insertObj).ExecuteCommand();
2017-07-05 22:57:54 +08:00
var t11 = db.Insertable(new MyStudent() { Id = 1, Name = "张三" }).AS("Student").ToSql();
2017-07-05 23:14:25 +08:00
base.Check(@"INSERT INTO [Student]
([Name])
VALUES
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
new SugarParameter("@Name","张三")
}, t11.Key, t11.Value, "Insert t11 error");
2017-07-25 01:13:31 +08:00
var t12 = db.Insertable<Student>(new { Name = "a" }).ToSql();
base.Check(@"INSERT INTO [STudent]
([Name])
VALUES
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
new SugarParameter("@Name","a")
}, t12.Key, t12.Value, "Insert t12 error");
var t13 = db.Insertable<Student>(new Dictionary<string, object>() { {"id",0 },{ "name","2"} }).ToSql();
base.Check(@"INSERT INTO [STudent]
([Name])
VALUES
(@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
new SugarParameter("@Name","2")
}, t13.Key, t13.Value, "Insert t13 error");
2017-04-30 22:49:41 +08:00
}
}
2017-07-05 22:57:54 +08:00
public class MyStudent {
public int Id { get; set; }
public string Name { get; set; }
}
2017-04-30 22:49:41 +08:00
}