using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OrmTest { public partial class NewUnitTest { public static void Updateable() { Db.CodeFirst.InitTables(typeof(UnitUser)); Db.DbMaintenance.TruncateTable(); Db.Insertable(new UnitUser() { USER_ID = 1, USER_ACCOUNT = "a", USER_PWD = "b", USER_NAME = "c", PWD_LASTCHTIME = DateTime.Now, PWD_ERRORCOUNT = 1, PWD_LASTERRTIME = DateTime.Now }).ExecuteCommand(); Db.Updateable(new UnitUser() { USER_ID = 1, PWD_LASTERRTIME = null }).WhereColumns(it => new { it.PWD_ERRORCOUNT, it.PWD_LASTERRTIME }).ExecuteCommand(); Db.CodeFirst.InitTables(typeof(UnitBoolTest)); var x = new UnitBoolTest(); Db.Updateable().SetColumns(it => new UnitBoolTest() { BoolValue = !it.BoolValue }).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable().SetColumns(it => it.BoolValue == !it.BoolValue).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable().SetColumns(it => new UnitBoolTest() { BoolValue = x.BoolValue }).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable().SetColumns(it => it.BoolValue == x.BoolValue).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable().SetColumns(it => new UnitBoolTest() { BoolValue = !x.BoolValue }).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable().SetColumns(it => it.BoolValue == !x.BoolValue).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable(x).ReSetValue(it => it.BoolValue = it.BoolValue).ExecuteCommand(); Db.Updateable(x).ReSetValue(it => it.BoolValue = true).ExecuteCommand(); Db.Updateable(x).ReSetValue(it => it.BoolValue = !it.BoolValue).ExecuteCommand(); Db.Updateable(x).UpdateColumns(it => new { it.BoolValue }).ExecuteCommand(); UnitSaveDiary saveDiary = new UnitSaveDiary(); saveDiary.ID = 2; saveDiary.TypeID = 10; saveDiary.TypeName = "类型100"; saveDiary.Title = "标题1000"; saveDiary.Content = "内容"; saveDiary.Time = DateTime.Now; saveDiary.IsRemind = false;//无论传false/true 最终执行的结果都是以true执行的 var sql = Db.Updateable().SetColumns(it => new UnitDiary() { IsRemind = saveDiary.IsRemind, }).Where(it => it.ID == saveDiary.ID).ToSql(); UValidate.Check(sql.Key, @"UPDATE [Diary] SET [IsRemind] = @Const0 WHERE ( [ID] = @ID1 )", "Updateable"); sql = Db.Updateable().SetColumns(it => new UnitDiary() { TypeID = saveDiary.TypeID, }).Where(it => it.ID == saveDiary.ID).ToSql(); UValidate.Check(sql.Key, @"UPDATE [Diary] SET [TypeID] = @Const0 WHERE ( [ID] = @ID1 )", "Updateable"); sql = Db.Updateable().SetColumns(it => new UnitDiary() { TypeID = saveDiary.TypeID, }).Where(it => it.ID == saveDiary.ID).ToSql(); UValidate.Check(sql.Key, @"UPDATE [Diary] SET [TypeID] = @Const0 WHERE ( [ID] = @ID1 )", "Updateable"); sql = Db.Updateable().SetColumns(it => new NullTest() { p = true }).Where(it => it.id == 1).ToSql(); UValidate.Check(sql.Key, @"UPDATE [NullTest] SET [p] = @Const0 WHERE ( [id] = @id1 )", "Updateable"); sql = Db.Updateable().SetColumns(it => new NullTest() { p2 = true }).Where(it => it.id == 1).ToSql(); UValidate.Check(sql.Key, @"UPDATE [NullTest] SET [p2] = @Const0 WHERE ( [id] = @id1 )", "Updateable"); Db.Updateable() .SetColumns(it => it.Name == "a") .SetColumns(it => it.CreateTime == DateTime.Now) .SetColumns(it => it.Price == 1).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable() .SetColumns(it => new Order { Name = "a", CreateTime = DateTime.Now }) .SetColumns(it => it.Price == 1).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable() .SetColumns(it => new Order { Name = "a", CreateTime = DateTime.Now }) .SetColumns(it => new Order() { Price = 1 }).Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable() .SetColumns(it => new Order { Name= it.Id>0 ?"1":"2", CreateTime = DateTime.Now }) .Where(it => it.Id == 1).ExecuteCommand(); Db.Updateable() .SetColumns(it => new Order { Name = SqlFunc.IsNull(it.Name,"a")+"b", CreateTime = DateTime.Now }) .Where(it => it.Id == 1).ExecuteCommand(); Db.CodeFirst.InitTables(); Db.Insertable(new Unitbluecopy()).UseSqlServer().ExecuteBulkCopy(); Db.Insertable(new Unitbluecopy()).UseSqlServer().ExecuteBulkCopy(); Db.CodeFirst.InitTables(); Db.Updateable() .SetColumns(it =>it.a== !it.a) .Where(it=>it.a) .ExecuteCommand(); Db.Updateable() .SetColumns(it=>new BoolTest1() { a = !it.a }) .Where(it => it.a) .ExecuteCommand(); Db.Queryable().Select(it => new Unitbluecopy() { x = it.x - SqlFunc.Subqueryable().Select(s => s.Id) }).ToList(); Db.Updateable() .SetColumns(it => new Unitbluecopy() { x=it.x- SqlFunc.Subqueryable().Select(s=>s.Id) }) .Where(it => it.Id>0) .ExecuteCommand(); Db.Updateable() .SetColumns(it => it.x == it.x - SqlFunc.Subqueryable().Select(s => s.Id) ) .Where(it => it.Id > 0) .ExecuteCommand(); Db.Updateable(new Unitbluecopy() { }).WhereColumns(it => it.Id).ExecuteCommand(); Db.Updateable(Db.Queryable().ToList()).WhereColumns(it => it.Id).ExecuteCommand(); } } public class Unitbluecopy { public int Id { get; set; } [SugarColumn(IsNullable =true)] public decimal? x { get; set; } } public class NullTest { public int id { get; set; } public bool? p { get; set; } public bool p2 { get; set; } } public class UnitSaveDiary { public int ID { get; set; } public int TypeID { get; set; } public string TypeName { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime? Time { get; set; } public bool IsRemind { get; set; } } /// /// 日记表 /// [SugarTable("Diary")] public class UnitDiary { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int ID { get; set; } /// /// 用户ID /// public int? UserID { get; set; } /// /// 日记类型ID /// public int? TypeID { get; set; } /// /// 日记类型名称 /// public string TypeName { get; set; } /// /// 标题 /// public string Title { get; set; } /// /// 内容 /// public string Content { get; set; } /// /// 时间 /// public DateTime? Time { get; set; } /// /// 是否提醒 /// public bool? IsRemind { get; set; } /// /// 封面图 /// public string Cover { get; set; } /// /// 是否为系统日记 1:系统日记 0:用户日记 /// public bool? IsSystem { get; set; } /// /// 权重(排序) /// public int? Sequence { get; set; } /// /// /// public string IP { get; set; } /// /// /// public DateTime? CreateTime { get; set; } /// /// /// public DateTime? UpdateTime { get; set; } /// /// /// public bool? IsDelete { get; set; } } public class UnitBoolTest { [SugarColumn(IsPrimaryKey =true)] public int Id { get; set; } public bool BoolValue { get; set; } public string Name { get; set; } } /// /// 普通用户表 /// [Serializable] public class UnitUser { private System.Int64? _USER_ID; /// /// GUID主键 /// [SugarColumn(IsPrimaryKey = true, IsIdentity = false)] public System.Int64? USER_ID { get { return this._USER_ID; } set { this._USER_ID = value; } } private System.String _USER_ACCOUNT; /// /// 用户账号,不可重名,即使是假删除了,亦不可重复 /// public System.String USER_ACCOUNT { get { return this._USER_ACCOUNT; } set { this._USER_ACCOUNT = value; } } private System.String _USER_PWD; /// /// 用户密码 /// public System.String USER_PWD { get { return this._USER_PWD; } set { this._USER_PWD = value; } } /// /// 不允许用户密码序列化,可以反序列化 /// /// public bool ShouldSerializeUSER_PWD() { return false; } private System.String _USER_NAME; /// /// 用户姓名 /// public System.String USER_NAME { get { return this._USER_NAME; } set { this._USER_NAME = value; } } private System.Int32 _USER_STATUS; /// /// 用户状体:10正常;20锁定;99已删除 /// public System.Int32 USER_STATUS { get { return this._USER_STATUS; } set { this._USER_STATUS = value; } } private System.DateTime _PWD_LASTCHTIME; /// /// 最后一次密码更新时间 /// public System.DateTime PWD_LASTCHTIME { get { return this._PWD_LASTCHTIME; } set { this._PWD_LASTCHTIME = value; } } private System.Int32? _PWD_ERRORCOUNT; /// /// 密码错误次数,达到定义的次数就锁定 /// public System.Int32? PWD_ERRORCOUNT { get { return this._PWD_ERRORCOUNT; } set { this._PWD_ERRORCOUNT = value ?? 0; } } private System.DateTime? _PWD_LASTERRTIME = null; /// /// 密码最后一次错误时间,满足定义的时间差之后,可自动解锁,如20分钟后自动解锁,亦作为累次错误次数的时间差比对基础 /// 允许为空 /// public System.DateTime? PWD_LASTERRTIME { get { return this._PWD_LASTERRTIME; } set { this._PWD_LASTERRTIME = value; } } } }