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.

120 lines
3.6 KiB

using OrmTest.Demo;
using OrmTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class VersionValidation : DemoBase
{
public static void Init()
6 years ago
{
6 years ago
TimestampDemo();
DateTimeDemo();
6 years ago
}
6 years ago
private static void TimestampDemo()
{
var db = GetInstance();
try
{
6 years ago
var data = new StudentVersion()
{
6 years ago
Id = db.Queryable<Student>().Select(it => it.Id).First(),
CreateTime = DateTime.Now,
Name = "",
};
db.Updateable(data).IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand();
var time = db.Queryable<StudentVersion>().Where(it => it.Id == data.Id).Select(it => it.Timestamp).Single();
data.Timestamp = time;
//is ok
db.Updateable(data).IsEnableUpdateVersionValidation().IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand();
//updated Timestamp change
//is error
db.Updateable(data).IsEnableUpdateVersionValidation().IgnoreColumns(it => new { it.Timestamp }).ExecuteCommand();
//IsEnableUpdateVersionValidation Types of support int or long or byte[](Timestamp) or Datetime
}
catch (Exception ex)
{
if (ex is SqlSugar.VersionExceptions)
{
Console.Write(ex.Message);
}
6 years ago
else
{
}
}
}
6 years ago
private static void DateTimeDemo()
6 years ago
{
var db = GetInstance();
try
{
var data = new StudentVersion2()
{
Id = db.Queryable<Student>().Select(it => it.Id).First(),
CreateTime = DateTime.Now,
Name = "",
};
db.Updateable(data).ExecuteCommand();
var time = db.Queryable<StudentVersion2>().Where(it => it.Id == data.Id).Select(it => it.CreateTime).Single();
data.CreateTime = time;
//is ok
db.Updateable(data).IsEnableUpdateVersionValidation().ExecuteCommand();
data.CreateTime = time.AddMilliseconds(-1);
//is error
db.Updateable(data).IsEnableUpdateVersionValidation().ExecuteCommand();
//IsEnableUpdateVersionValidation Types of support int or long or byte[](Timestamp) or Datetime
}
catch (Exception ex)
{
if (ex is SqlSugar.VersionExceptions)
{
Console.Write(ex.Message);
}
else
{
}
}
}
6 years ago
[SqlSugar.SugarTable("Student")]
public class StudentVersion
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
6 years ago
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true,IsOnlyIgnoreInsert=true)]
public byte[] Timestamp { get; set; }
}
6 years ago
[SqlSugar.SugarTable("Student")]
public class StudentVersion2
{
public int Id { get; set; }
public string Name { get; set; }
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true, IsOnlyIgnoreInsert = true)]
public DateTime CreateTime { get; set; }
}
}
}