SqlSugar/OrmTest/UnitTest/Update.cs

64 lines
2.1 KiB
C#
Raw Normal View History

2017-05-17 00:22:29 +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-19 15:21:24 +08:00
public class Update : UnitTestBase
2017-05-17 00:22:29 +08:00
{
private Update() { }
public Update(int eachCount)
{
this.Count = eachCount;
}
public void Init()
{
var db = GetInstance();
2017-05-19 15:21:24 +08:00
var updateObj = new Student() { Id = 1, Name = "jack", CreateTime = DateTime.Now };
2017-05-17 00:22:29 +08:00
var updateObjs = new List<Student>() { updateObj }.ToArray();
db.IgnoreColumns.Add("TestId", "Student");
//db.MappingColumns.Add("id","dbid", "Student");
var s1 = db.Updateable(updateObj).ToSql();
2017-05-19 15:21:24 +08:00
//update reutrn Command Count
2017-05-17 00:22:29 +08:00
var s2 = db.Updateable(updateObj).ExecuteCommand();
db.IgnoreColumns = null;
2017-05-19 15:21:24 +08:00
//Only update Name
var s3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ToSql();
2017-05-17 00:22:29 +08:00
//Ignore Name and TestId
var s4 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
//Ignore Name and TestId
var s5 = db.Updateable(updateObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
//Use Lock
var s6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ToSql();
//ToSql
2017-05-19 15:21:24 +08:00
var s7 = db.Updateable(updateObj).With(SqlWith.UpdLock)
.UpdateColumns(it => new { it.Name }).ToSql();
2017-05-17 00:22:29 +08:00
2017-05-19 15:21:24 +08:00
//update List<T>
2017-05-17 00:22:29 +08:00
var s8 = db.Updateable(updateObj).With(SqlWith.UpdLock).ToSql();
2017-05-19 15:21:24 +08:00
//Re Set Value
var s9 = db.Updateable(updateObj)
.ReSetValue(it=>it.Name==(it.Name+1)).ToSql();
2017-05-17 00:22:29 +08:00
}
public SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new SystemTablesConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
return db;
}
}
}