sunkaixuan
6 years ago
12 changed files with 310 additions and 11 deletions
@ -0,0 +1,43 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using SqlSugar; |
|||
namespace OrmTest.Demo |
|||
{ |
|||
public class AttributeDemo : DemoBase |
|||
{ |
|||
public static void Init() |
|||
{ |
|||
var db = GetInstance(); |
|||
AttributeTest a = new AttributeTest() |
|||
{ |
|||
Name = "attr" |
|||
}; |
|||
db.Insertable(a).AS("student").ExecuteCommand(); |
|||
var list = db.Queryable<AttributeTest>().AS("student").ToList(); |
|||
var list2 = db.Queryable<AttributeTest>().AS("student").Select(it => new AttributeTest() { Aid = it.Aid + 1,CreateTime=DateTime.Now,Name=it.Name }).ToList(); |
|||
var s = new AttributeTest2() { Aid = 1,AName="a", CreateTime=DateTime.Now }; |
|||
var count = db.Updateable(s).UpdateColumns(it=>new { it.CreateTime,it.AName }).Where(it=>it.Aid==100).ExecuteCommand(); |
|||
} |
|||
|
|||
public class AttributeTest |
|||
{ |
|||
[SugarColumn(ColumnName = "Id")] |
|||
public int Aid { get; set; } |
|||
public string Name { get; set; } |
|||
[SugarColumn(IsOnlyIgnoreInsert = true)] |
|||
public DateTime CreateTime { get; set; } |
|||
} |
|||
[SugarTable("student")] |
|||
public class AttributeTest2 |
|||
{ |
|||
[SugarColumn(ColumnName = "Id")] |
|||
public int Aid { get; set; } |
|||
[SugarColumn(ColumnName = "Name")] |
|||
public string AName { get; set; } |
|||
[SugarColumn(IsOnlyIgnoreInsert = true)] |
|||
public DateTime CreateTime { get; set; } |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,119 @@ |
|||
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() |
|||
{ |
|||
TimestampDemo(); |
|||
DateTimeDemo(); |
|||
} |
|||
|
|||
private static void TimestampDemo() |
|||
{ |
|||
var db = GetInstance(); |
|||
try |
|||
{ |
|||
|
|||
var data = new StudentVersion() |
|||
{ |
|||
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); |
|||
} |
|||
else |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
private static void DateTimeDemo() |
|||
{ |
|||
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 |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
[SqlSugar.SugarTable("Student")] |
|||
public class StudentVersion |
|||
{ |
|||
public int Id { get; set; } |
|||
public string Name { get; set; } |
|||
public DateTime CreateTime { get; set; } |
|||
[SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true,IsOnlyIgnoreInsert=true)] |
|||
public byte[] Timestamp { get; set; } |
|||
} |
|||
|
|||
[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; } |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,64 @@ |
|||
using OrmTest.Models; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
namespace OrmTest.Demo |
|||
{ |
|||
public class Mapper : DemoBase |
|||
{ |
|||
public static void Init() |
|||
{ |
|||
var db = GetInstance(); |
|||
|
|||
//auto fill ViewModelStudent3
|
|||
var s11 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id) |
|||
.Select<ViewModelStudent3>().ToList(); |
|||
|
|||
|
|||
var s12 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>() |
|||
|
|||
.Mapper((it, cache) => |
|||
{ |
|||
|
|||
var allSchools = cache.GetListByPrimaryKeys<School>(vmodel => vmodel.SchoolId); |
|||
//sql select shool where id (in(ViewModelStudent3[0].SchoolId , ViewModelStudent3[1].SchoolId...)
|
|||
|
|||
//Equal to allSchools
|
|||
//var allSchools2= cache.Get(list =>
|
|||
// {
|
|||
// var ids=list.Select(i => it.SchoolId).ToList();
|
|||
// return db.Queryable<School>().In(ids).ToList();
|
|||
//});Complex writing metho
|
|||
|
|||
|
|||
/*one to one*/ |
|||
//Good performance
|
|||
it.School = allSchools.FirstOrDefault(i => i.Id == it.SchoolId); |
|||
|
|||
//Poor performance.
|
|||
//it.School = db.Queryable<School>().InSingle(it.SchoolId);
|
|||
|
|||
|
|||
/*one to many*/ |
|||
it.Schools = allSchools.Where(i => i.Id == it.SchoolId).ToList(); |
|||
|
|||
|
|||
/*C# syntax conversion*/ |
|||
it.Name = it.Name == null ? "null" : it.Name; |
|||
|
|||
}).ToList(); |
|||
|
|||
|
|||
var s13 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select<ViewModelStudent3>() |
|||
|
|||
.Mapper((it, cache) => |
|||
{ |
|||
it.Schools = db.Queryable<School>().Where(i => i.Id == it.SchoolId).ToList(); |
|||
}).ToList(); |
|||
|
|||
|
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue