SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
6.5 KiB

8 years ago
using OrmTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest.UnitTest
{
8 years ago
public class Update : UnitTestBase
8 years ago
{
private Update() { }
public Update(int eachCount)
{
this.Count = eachCount;
}
public void Init()
{
var db = GetInstance();
8 years ago
var updateObj = new Student() { Id = 1, Name = "jack",SchoolId=0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") };
var updateObjs = new List<Student>() { updateObj,new Student() { Id=2,Name="sun",SchoolId=0 } }.ToArray();
8 years ago
db.IgnoreColumns.Add("TestId", "Student");
//db.MappingColumns.Add("id","dbid", "Student");
8 years ago
var t1 = db.Updateable(updateObj).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] SET
8 years ago
[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
new SugarParameter("@SchoolId",0),
8 years ago
new SugarParameter("@ID",1),
8 years ago
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
new SugarParameter("@Name", "jack")
}, t1.Key, t1.Value,"Update t1 error");
8 years ago
8 years ago
//update reutrn Command Count
8 years ago
var t2 = db.Updateable(updateObj).ExecuteCommand();
8 years ago
db.IgnoreColumns = null;
8 years ago
//Only update Name
8 years ago
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] SET
8 years ago
[Name]=@Name WHERE [Id]=@Id", new List<SugarParameter>() {
8 years ago
new SugarParameter("@ID",1),
8 years ago
new SugarParameter("@Name", "jack")
}, t3.Key, t3.Value, "Update t3 error");
8 years ago
//Ignore Name and TestId
8 years ago
var t4 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] SET
8 years ago
[SchoolId]=@SchoolId,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
new SugarParameter("@CreateTime",Convert.ToDateTime("2017-05-21 09:56:12.610")),
new SugarParameter("@SchoolId", 0),
8 years ago
new SugarParameter("@ID",1),
8 years ago
}, t4.Key, t4.Value, "Update t4 error");
8 years ago
//Ignore Name and TestId
8 years ago
var t5 = db.Updateable(updateObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] WITH(UPDLOCK) SET
8 years ago
[SchoolId]=@SchoolId,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
new SugarParameter("@CreateTime",Convert.ToDateTime("2017-05-21 09:56:12.610")),
new SugarParameter("@SchoolId", 0),
8 years ago
new SugarParameter("@ID",1),
8 years ago
}, t5.Key, t5.Value, "Update t5 error");
8 years ago
//Use Lock
8 years ago
var t6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] WITH(UPDLOCK) SET
[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
8 years ago
new SugarParameter("@SchoolId",0),
8 years ago
new SugarParameter("@ID",1),
8 years ago
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
new SugarParameter("@Name", "jack")
}, t6.Key, t6.Value, "Update t6 error");
8 years ago
8 years ago
//update List<T>
8 years ago
var t7 = db.Updateable(updateObjs).With(SqlWith.UpdLock).ToSql();
8 years ago
base.Check(@"UPDATE S SET S.[SchoolId]=T.[SchoolId],S.[Name]=T.[Name],S.[CreateTime]=T.[CreateTime] FROM [STudent] S WITH(UPDLOCK) INNER JOIN (
8 years ago
8 years ago
SELECT N'1' AS ID,N'0' AS SchoolId,N'jack' AS Name,'2017-05-21 09:56:12.610' AS CreateTime
8 years ago
UNION ALL
8 years ago
SELECT N'2' AS ID,N'0' AS SchoolId,N'sun' AS Name,NULL AS CreateTime
8 years ago
) T ON S.[Id]=T.[Id]
8 years ago
; ", null, t7.Key, null,"Update t7 error");
8 years ago
//Re Set Value
8 years ago
var t8 = db.Updateable(updateObj)
8 years ago
.ReSetValue(it=>it.Name==(it.Name+1)).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] SET
[SchoolId]=@SchoolId, [Name] =( [Name] + @Const0 ),[CreateTime]=@CreateTime WHERE [Id]=@Id",
8 years ago
new List<SugarParameter>() {
new SugarParameter("@SchoolId",0),
8 years ago
new SugarParameter("@ID",1),
8 years ago
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
8 years ago
new SugarParameter("@Const0", 1)
8 years ago
}, t8.Key, t8.Value, "Update t8 error"
);
8 years ago
//Where By Expression
8 years ago
var t9 = db.Updateable(updateObj)
8 years ago
.Where(it => it.Id==1).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] SET
[SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE ( [ID] = @Id0 )",
8 years ago
new List<SugarParameter>() {
new SugarParameter("@SchoolId",0),
8 years ago
new SugarParameter("@ID",1),
8 years ago
new SugarParameter("@Id0",1),
new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
new SugarParameter("@Name", "jack") },t9.Key,t9.Value,"Upate t9 error"
);
8 years ago
updateObj.SchoolId = 18;
string name = "x";
var t10 = db.Updateable<Student>().UpdateColumns(it => new Student() { Name =name, SchoolId=updateObj.SchoolId }).Where(it=>it.Id==11).ToSql();
8 years ago
base.Check(@"UPDATE [STudent] SET
[SchoolId] = @Const0 , [Name] = @const3 WHERE ( [ID] = @Id1 )", new List<SugarParameter>() {
8 years ago
new SugarParameter("@const3","x"),
8 years ago
new SugarParameter("@Const0",18),
new SugarParameter("@Id1",11)},
t10.Key,
t10.Value,
"Update 10 error"
);
8 years ago
}
public SqlSugarClient GetInstance()
{
8 years ago
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
8 years ago
return db;
}
}
}