mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Add HGTest
This commit is contained in:
parent
dc28ceeeba
commit
a61ad92a9b
227
Src/Asp.NetCore2/HGTest/Bugs/BugTest.cs
Normal file
227
Src/Asp.NetCore2/HGTest/Bugs/BugTest.cs
Normal file
@ -0,0 +1,227 @@
|
||||
//using SqlSugar;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.ComponentModel.DataAnnotations;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace OrmTest.Test
|
||||
//{
|
||||
// public class BugTest
|
||||
// {
|
||||
// public static void Init()
|
||||
// {
|
||||
// SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
|
||||
// {
|
||||
// ConnectionString = @"PORT=5433;DATABASE=x;HOST=localhost;PASSWORD=haosql;USER ID=postgres",
|
||||
// DbType = DbType.HG,
|
||||
// IsAutoCloseConnection = true,
|
||||
// //MoreSettings = new ConnMoreSettings()
|
||||
// //{
|
||||
// // PgSqlIsAutoToLower = true //我们这里需要设置为false
|
||||
// //},
|
||||
// InitKeyType = InitKeyType.Attribute,
|
||||
// });
|
||||
// //调式代码 用来打印SQL
|
||||
// Db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
// {
|
||||
// // Debug.WriteLine(sql);
|
||||
// };
|
||||
|
||||
// var 查询成功 = Db.Queryable<Sys_Menu>()
|
||||
// .Where(x => x.Enable == true) //不加 ture ,也会出错
|
||||
// .ToList();
|
||||
|
||||
|
||||
// var Dbfirst情况使用Mapper_Menu_Id为大写自动转成小写 = Db.Queryable<Sys_Menu>()
|
||||
// .Where(x => x.Enable == true)
|
||||
// .Mapper(x => x.orderList, x => x.Menu_Id) //初始化数据库失败42703: 字段 "menu_id" 不存在
|
||||
// .ToList();
|
||||
// }
|
||||
// }
|
||||
// public class Sys_RoleAuth
|
||||
// {
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Key]
|
||||
// [Display(Name = "")]
|
||||
// [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
// public int Auth_Id { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "")]
|
||||
// public int Role_Id { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "")]
|
||||
// public int User_Id { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "")]
|
||||
// [SugarColumn(IsNullable = false)]
|
||||
// public int Menu_Id { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///用户权限
|
||||
// /// </summary>
|
||||
// [Display(Name = "用户权限")]
|
||||
// [SugarColumn(IsNullable = false, IsJson = true, ColumnDataType = "json")]
|
||||
// public List<Sys_Actions> AuthValue { get; set; } = new List<Sys_Actions>();
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "")]
|
||||
// [MaxLength(100)]
|
||||
// public string Creator { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "")]
|
||||
// public DateTime CreateDate { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "")]
|
||||
// [MaxLength(100)]
|
||||
// public string Modifier { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "")]
|
||||
// public DateTime ModifyDate { get; set; }
|
||||
// }
|
||||
// public class Sys_Actions
|
||||
// {
|
||||
// public int Action_Id { get; set; }
|
||||
// public int Menu_Id { get; set; }
|
||||
// public string Text { get; set; }
|
||||
// public string Value { get; set; }
|
||||
// }
|
||||
// public class Sys_Menu
|
||||
// {
|
||||
// /// <summary>
|
||||
// ///ID
|
||||
// /// </summary>
|
||||
// [Key]
|
||||
// [Display(Name = "ID")]
|
||||
// [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
// public int Menu_Id { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///父级ID
|
||||
// /// </summary>
|
||||
// [Display(Name = "父级ID")]
|
||||
// [SugarColumn(IsNullable = false)]
|
||||
// public int ParentId { get; set; }
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// ///菜单名称
|
||||
// /// </summary>
|
||||
// [Display(Name = "菜单名称")]
|
||||
// [MaxLength(50)]
|
||||
// [SugarColumn(IsNullable = false)]
|
||||
// public string MenuName { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "TableName")]
|
||||
// [MaxLength(200)]
|
||||
// public string TableName { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "Url")]
|
||||
// [MaxLength(10000)]
|
||||
// public string Url { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///权限
|
||||
// /// </summary>
|
||||
// [Display(Name = "权限")]
|
||||
// [MaxLength(-1)]
|
||||
// [SugarColumn(IsNullable = false, IsJson = true, ColumnDataType = "json")]
|
||||
// public List<Sys_Actions> Auth { get; set; } = new List<Sys_Actions>();
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "Description")]
|
||||
// [MaxLength(200)]
|
||||
// public string Description { get; set; }
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// ///图标
|
||||
// /// </summary>
|
||||
// [Display(Name = "图标")]
|
||||
// [MaxLength(50)]
|
||||
// public string Icon { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///排序号
|
||||
// /// </summary>
|
||||
// [Display(Name = "排序号")]
|
||||
// public int OrderNo { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///创建人
|
||||
// /// </summary>
|
||||
// [Display(Name = "创建人")]
|
||||
// [MaxLength(50)]
|
||||
// public string Creator { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///创建时间
|
||||
// /// </summary>
|
||||
// [Display(Name = "创建时间")]
|
||||
// public DateTime? CreateDate { get; set; } = DateTime.Now;
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "Modifier")]
|
||||
// [MaxLength(50)]
|
||||
// public string Modifier { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// [Display(Name = "ModifyDate")]
|
||||
// public DateTime? ModifyDate { get; set; }
|
||||
|
||||
// /// <summary>
|
||||
// ///是否启用
|
||||
// /// </summary>
|
||||
// [Display(Name = "是否启用")]
|
||||
// public bool Enable { get; set; }
|
||||
|
||||
// [SugarColumn(IsIgnore = true)]
|
||||
// public List<Order> orderList { get; set; }
|
||||
// }
|
||||
|
||||
// public class Order
|
||||
// {
|
||||
// [Key]
|
||||
// [Display(Name = "ID")]
|
||||
// [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
// public int Id { get; set; }
|
||||
|
||||
// public int Menu_Id { get; set; }
|
||||
// }
|
||||
|
||||
//}
|
53
Src/Asp.NetCore2/HGTest/Bugs/BugTest2.cs
Normal file
53
Src/Asp.NetCore2/HGTest/Bugs/BugTest2.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.Test
|
||||
{
|
||||
public class BugTest
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = @"PORT=5433;DATABASE=x;HOST=localhost;PASSWORD=haosql;USER ID=postgres",
|
||||
DbType = DbType.HG,
|
||||
IsAutoCloseConnection = true,
|
||||
//MoreSettings = new ConnMoreSettings()
|
||||
//{
|
||||
// PgSqlIsAutoToLower = true //我们这里需要设置为false
|
||||
//},
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
});
|
||||
//调式代码 用来打印SQL
|
||||
Db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
// Debug.WriteLine(sql);
|
||||
};
|
||||
|
||||
Db.CodeFirst.InitTables(typeof(My12311x1));
|
||||
|
||||
var id = Db.Insertable(new My12311x1 { Menu_Id = new int[] { 1, 2 } }).ExecuteReturnIdentity();
|
||||
var list = Db.Queryable<My12311x1>().InSingle(id);
|
||||
list.Menu_Id = new int[] { 3 };
|
||||
Db.Updateable(list).ExecuteCommand();
|
||||
list = Db.Queryable<My12311x1>().InSingle(id);
|
||||
}
|
||||
}
|
||||
|
||||
public class My12311x1
|
||||
{
|
||||
[Key]
|
||||
[Display(Name = "ID")]
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
[SugarColumn(ColumnDataType = "int []", IsArray = true)]
|
||||
public int[] Menu_Id { get; set; }
|
||||
}
|
||||
|
||||
}
|
46
Src/Asp.NetCore2/HGTest/Bugs/BugTest3.cs
Normal file
46
Src/Asp.NetCore2/HGTest/Bugs/BugTest3.cs
Normal file
@ -0,0 +1,46 @@
|
||||
//using SqlSugar;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.ComponentModel.DataAnnotations;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace OrmTest.Test
|
||||
//{
|
||||
// public class BugTest
|
||||
// {
|
||||
// public static void Init()
|
||||
// {
|
||||
// SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
|
||||
// {
|
||||
// ConnectionString = @"PORT=5433;DATABASE=x;HOST=localhost;PASSWORD=haosql;USER ID=postgres",
|
||||
// DbType = DbType.HG,
|
||||
// IsAutoCloseConnection = true,
|
||||
|
||||
// InitKeyType = InitKeyType.Attribute,
|
||||
// });
|
||||
// //调式代码 用来打印SQL
|
||||
// Db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
// {
|
||||
// Console.WriteLine(sql);
|
||||
// };
|
||||
|
||||
// Db.CodeFirst.InitTables(typeof(testmmmm121));
|
||||
|
||||
// var id = Db.Insertable(new testmmmm121 { name = "a", isok = true }).ExecuteReturnIdentity();
|
||||
// var list = Db.Queryable<testmmmm121>().InSingle(id);
|
||||
// var x = Db.Queryable<testmmmm121>().Where(it => !it.isok).ToList();
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// public class testmmmm121
|
||||
// {
|
||||
// [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
// public int id { get; set; }
|
||||
// public string name { get; set; }
|
||||
// public bool isok { get; set; }
|
||||
// }
|
||||
|
||||
//}
|
31
Src/Asp.NetCore2/HGTest/Config.cs
Normal file
31
Src/Asp.NetCore2/HGTest/Config.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
/// <summary>
|
||||
/// Setting up the database name does not require you to create the database
|
||||
/// 设置好数据库名不需要你去手动建库
|
||||
/// </summary>
|
||||
public class Config
|
||||
{
|
||||
/// <summary>
|
||||
/// Account have permission to create database
|
||||
/// 用有建库权限的数据库账号
|
||||
/// </summary>
|
||||
public static string ConnectionString = "Server=27.151.1.54;Port=5866;UId=design;Password=Hello@12345;Database=design; searchpath=design";
|
||||
/// <summary>
|
||||
/// Account have permission to create database
|
||||
/// 用有建库权限的数据库账号
|
||||
/// </summary>
|
||||
public static string ConnectionString2 = ConnectionString;
|
||||
/// <summary>
|
||||
/// Account have permission to create database
|
||||
/// 用有建库权限的数据库账号
|
||||
/// </summary>
|
||||
public static string ConnectionString3 = ConnectionString;
|
||||
}
|
||||
}
|
425
Src/Asp.NetCore2/HGTest/Demo/Demo0_SqlSugarClient.cs
Normal file
425
Src/Asp.NetCore2/HGTest/Demo/Demo0_SqlSugarClient.cs
Normal file
@ -0,0 +1,425 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Nhgdb;
|
||||
using SqlSugar;
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo0_SqlSugarClient
|
||||
{
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
SqlSugarClient();//Create db
|
||||
DbContext();//Optimizing SqlSugarClient usage
|
||||
SingletonPattern();//Singleten Pattern
|
||||
DistributedTransactionExample();
|
||||
MasterSlave();//Read-write separation
|
||||
CustomAttribute();
|
||||
}
|
||||
|
||||
private static void MasterSlave()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### MasterSlave Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,//Master Connection
|
||||
DbType = DbType.HG,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
SlaveConnectionConfigs = new List<SlaveConnectionConfig>() {
|
||||
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 } ,
|
||||
new SlaveConnectionConfig() { HitRate=10, ConnectionString=Config.ConnectionString2 }
|
||||
}
|
||||
});
|
||||
db.Aop.OnLogExecuted = (s, p) =>
|
||||
{
|
||||
Console.WriteLine(db.Ado.Connection.ConnectionString);
|
||||
};
|
||||
Console.WriteLine("Master:");
|
||||
db.Insertable(new Order() { Name = "abc", CustomId = 1, CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
Console.WriteLine("Slave:");
|
||||
db.Queryable<Order>().First();
|
||||
Console.WriteLine("#### MasterSlave End ####");
|
||||
}
|
||||
|
||||
private static void SqlSugarClient()
|
||||
{
|
||||
//Create db
|
||||
Console.WriteLine("#### SqlSugarClient Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
new NhgdbConnection(Config.ConnectionString)
|
||||
.Open();
|
||||
|
||||
//If no exist create datebase
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
|
||||
//Use db query
|
||||
var dt = db.Ado.GetDataTable("select 1");
|
||||
|
||||
//Create tables
|
||||
db.CodeFirst.InitTables(typeof(OrderItem),typeof(Order));
|
||||
var id = db.Insertable(new Order() { Name = "order1", CustomId = 1, Price = 0, CreateTime = DateTime.Now }).ExecuteReturnIdentity();
|
||||
|
||||
//Insert data
|
||||
db.Insertable(new OrderItem() { OrderId = id, Price = 0, CreateTime=DateTime.Now }).ExecuteCommand();
|
||||
Console.WriteLine("#### SqlSugarClient End ####");
|
||||
|
||||
}
|
||||
|
||||
private static void DbContext()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### DbContext Start ####");
|
||||
var insertObj = new Order { Name = "jack", CreateTime = DateTime.Now };
|
||||
var InsertObjs = new Order[] { insertObj };
|
||||
|
||||
DbContext context = new DbContext();
|
||||
|
||||
context.Db.CodeFirst.InitTables<Order, OrderItem,Custom>();//Create Tables
|
||||
;
|
||||
var orderDb = context.OrderDb;
|
||||
|
||||
//Select
|
||||
var data1 = orderDb.GetById(1);
|
||||
var data2 = orderDb.GetList();
|
||||
var data3 = orderDb.GetList(it => it.Id == 1);
|
||||
var data4 = orderDb.GetSingle(it => it.Id == 1);
|
||||
var p = new PageModel() { PageIndex = 1, PageSize = 2 };
|
||||
var data5 = orderDb.GetPageList(it => it.Name == "xx", p);
|
||||
Console.Write(p.TotalCount);
|
||||
var data6 = orderDb.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc);
|
||||
Console.Write(p.TotalCount);
|
||||
List<IConditionalModel> conModels = new List<IConditionalModel>();
|
||||
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1", FieldValueConvertFunc=it=>Convert.ToInt32(it) });//id=1
|
||||
var data7 = orderDb.GetPageList(conModels, p, it => it.Name, OrderByType.Asc);
|
||||
orderDb.AsQueryable().Where(x => x.Id == 1).ToList();
|
||||
|
||||
//Insert
|
||||
orderDb.Insert(insertObj);
|
||||
orderDb.InsertRange(InsertObjs);
|
||||
var id = orderDb.InsertReturnIdentity(insertObj);
|
||||
orderDb.AsInsertable(insertObj).ExecuteCommand();
|
||||
|
||||
|
||||
//Delete
|
||||
orderDb.Delete(insertObj);
|
||||
orderDb.DeleteById(11111);
|
||||
orderDb.DeleteById(new int[] { 1111, 2222 });
|
||||
orderDb.Delete(it => it.Id == 1111);
|
||||
orderDb.AsDeleteable().Where(it => it.Id == 1111).ExecuteCommand();
|
||||
|
||||
//Update
|
||||
orderDb.Update(insertObj);
|
||||
orderDb.UpdateRange(InsertObjs);
|
||||
orderDb.Update(it => new Order() { Name = "a", }, it => it.Id == 1);
|
||||
orderDb.AsUpdateable(insertObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
|
||||
|
||||
//Use Inherit DbContext
|
||||
OrderDal dal = new OrderDal();
|
||||
var data = dal.GetById(1);
|
||||
var list = dal.GetList();
|
||||
|
||||
Console.WriteLine("#### DbContext End ####");
|
||||
}
|
||||
|
||||
private static void CustomAttribute()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Custom Attribute Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.HG,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
EntityService = (property, column) =>
|
||||
{
|
||||
|
||||
var attributes = property.GetCustomAttributes(true);//get all attributes
|
||||
|
||||
if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
|
||||
{
|
||||
column.IsPrimarykey = true;
|
||||
}
|
||||
},
|
||||
EntityNameService = (type, entity) =>
|
||||
{
|
||||
var attributes = type.GetCustomAttributes(true);
|
||||
if (attributes.Any(it => it is TableAttribute))
|
||||
{
|
||||
entity.DbTableName = (attributes.First(it => it is TableAttribute) as TableAttribute).Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
db.CodeFirst.InitTables<AttributeTable>();//Create Table
|
||||
|
||||
db.Insertable(new AttributeTable() { Id = Guid.NewGuid().ToString(), Name = "Name" }).ExecuteCommand();
|
||||
var list = db.Queryable<AttributeTable>().ToList();
|
||||
|
||||
Console.WriteLine("#### Custom Attribute End ####");
|
||||
}
|
||||
|
||||
|
||||
private static void SingletonPattern()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Singleton Pattern Start ####");
|
||||
Console.WriteLine("Db_Id:" + singleDb.ContextID);
|
||||
Console.WriteLine("Db_Id:" + singleDb.ContextID);
|
||||
var task = new Task(() =>
|
||||
{
|
||||
Console.WriteLine("Task DbId:" + singleDb.ContextID);
|
||||
new Task(() =>
|
||||
{
|
||||
Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID);
|
||||
Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID);
|
||||
|
||||
}).Start();
|
||||
Console.WriteLine("Task DbId:" + singleDb.ContextID);
|
||||
});
|
||||
task.Start();
|
||||
task.Wait();
|
||||
System.Threading.Thread.Sleep(500);
|
||||
Console.WriteLine(string.Join(",", singleDb.TempItems.Keys));
|
||||
|
||||
Console.WriteLine("#### Singleton Pattern end ####");
|
||||
}
|
||||
|
||||
static SqlSugarScope singleDb = new SqlSugarScope(
|
||||
new ConnectionConfig()
|
||||
{
|
||||
ConfigId = 1,
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents()
|
||||
{
|
||||
OnLogExecuting = (sql, p) => { Console.WriteLine(sql); }
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private static void DistributedTransactionExample()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Distributed TransactionExample Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
|
||||
{
|
||||
new ConnectionConfig(){ ConfigId="1", DbType=DbType.HG, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true },
|
||||
new ConnectionConfig(){ ConfigId="2", DbType=DbType.HG, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true}
|
||||
});
|
||||
|
||||
var db1 = db.Ado.Connection.Database;
|
||||
//use db1
|
||||
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));//
|
||||
db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable<Order>().Count());
|
||||
|
||||
//use db2
|
||||
db.ChangeDatabase("2");
|
||||
var db2 = db.Ado.Connection.Database;
|
||||
db.DbMaintenance.CreateDatabase();//Create Database2
|
||||
db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));
|
||||
db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType + ":" + db.Queryable<Order>().Count());
|
||||
|
||||
if (db2 == db1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Example 1
|
||||
Console.WriteLine("Example 1");
|
||||
try
|
||||
{
|
||||
db.BeginTran();
|
||||
|
||||
db.ChangeDatabase("1");//use db1
|
||||
db.Deleteable<Order>().ExecuteCommand();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase("2");//use db2
|
||||
db.Deleteable<Order>().ExecuteCommand();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
throw new Exception();
|
||||
db.CommitTran();
|
||||
}
|
||||
catch
|
||||
{
|
||||
db.RollbackTran();
|
||||
Console.WriteLine("---Roll back");
|
||||
db.ChangeDatabase("1");//use db1
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase("2");//use db2
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Example 2
|
||||
Console.WriteLine("Example 2");
|
||||
|
||||
var result=db.UseTran(() =>
|
||||
{
|
||||
|
||||
db.ChangeDatabase("1");//use db1
|
||||
db.Deleteable<Order>().ExecuteCommand();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase("2");//use db2
|
||||
db.Deleteable<Order>().ExecuteCommand();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
throw new Exception("");
|
||||
|
||||
});
|
||||
if (result.IsSuccess == false) {
|
||||
Console.WriteLine("---Roll back");
|
||||
db.ChangeDatabase("1");//use db1
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase("2");//use db2
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
}
|
||||
|
||||
// Example 3
|
||||
Console.WriteLine("Example 3");
|
||||
|
||||
var result2 = db.UseTranAsync(async () =>
|
||||
{
|
||||
|
||||
db.ChangeDatabase("1");//use db1
|
||||
await db.Deleteable<Order>().ExecuteCommandAsync();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase("2");//use db2
|
||||
await db.Deleteable<Order>().ExecuteCommandAsync();
|
||||
Console.WriteLine("---Delete all " + db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
throw new Exception("");
|
||||
|
||||
});
|
||||
result2.Wait();
|
||||
if (result2.Result.IsSuccess == false)
|
||||
{
|
||||
Console.WriteLine("---Roll back");
|
||||
db.ChangeDatabase("1");//use sqlserver
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
|
||||
db.ChangeDatabase("2");//use mysql
|
||||
Console.WriteLine(db.CurrentConnectionConfig.DbType);
|
||||
Console.WriteLine(db.Queryable<Order>().Count());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DbContext Example 1
|
||||
/// </summary>
|
||||
public class DbContext
|
||||
{
|
||||
|
||||
public SqlSugarClient Db;
|
||||
public DbContext()
|
||||
{
|
||||
Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.HG,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
AopEvents = new AopEvents()
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
public SimpleClient<Order> OrderDb => new SimpleClient<Order>(Db);
|
||||
public SimpleClient<OrderItem> OrderItemDb => new SimpleClient<OrderItem>(Db);
|
||||
}
|
||||
|
||||
|
||||
public class OrderDal : DbContext<Order>
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// DbContext Example 2
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class DbContext<T> where T : class, new()
|
||||
{
|
||||
|
||||
public SqlSugarClient Db;
|
||||
public DbContext()
|
||||
{
|
||||
Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.HG,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
AopEvents = new AopEvents()
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
public SimpleClient<T> CurrentDb => new SimpleClient<T>(Db);
|
||||
public virtual T GetById(int id)
|
||||
{
|
||||
return CurrentDb.GetById(id);
|
||||
}
|
||||
public virtual List<T> GetList()
|
||||
{
|
||||
return CurrentDb.GetList();
|
||||
}
|
||||
public virtual bool Delete(int id)
|
||||
{
|
||||
return CurrentDb.DeleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
342
Src/Asp.NetCore2/HGTest/Demo/Demo1_Queryable.cs
Normal file
342
Src/Asp.NetCore2/HGTest/Demo/Demo1_Queryable.cs
Normal file
@ -0,0 +1,342 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo1_Queryable
|
||||
{
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
EasyExamples();
|
||||
QueryConditions();
|
||||
JoinTable();
|
||||
Async();
|
||||
NoEntity();
|
||||
Mapper();
|
||||
SqlFuncTest();
|
||||
Subquery();
|
||||
ReturnType();
|
||||
}
|
||||
|
||||
private static void EasyExamples()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Examples Start ####");
|
||||
var db = GetInstance();
|
||||
var dbTime = db.GetDate();
|
||||
var getAll = db.Queryable<Order>().ToList();
|
||||
var getOrderBy = db.Queryable<Order>().OrderBy(it => it.Name,OrderByType.Desc).ToList();
|
||||
var getOrderBy2 = db.Queryable<Order>().OrderBy(it => it.Id).OrderBy(it => it.Name, OrderByType.Desc).ToList();
|
||||
var getOrderBy3 = db.Queryable<Order>().OrderBy(it =>new { it.Name,it.Id}).ToList();
|
||||
var getRandom = db.Queryable<Order>().OrderBy(it => SqlFunc.GetRandom()).First();
|
||||
var getByPrimaryKey = db.Queryable<Order>().InSingle(2);
|
||||
var getSingleOrDefault = db.Queryable<Order>().Where(it => it.Id == 1).Single();
|
||||
var getFirstOrDefault = db.Queryable<Order>().First();
|
||||
var getByWhere = db.Queryable<Order>().Where(it => it.Id == 1 || it.Name == "a").ToList();
|
||||
var getByWhere2 = db.Queryable<Order>().Where(it => it.Id == DateTime.Now.Year).ToList();
|
||||
var getByFuns = db.Queryable<Order>().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();
|
||||
var getByFuns2 = db.Queryable<Order>().GroupBy(it => it.Name).Select(it => SqlFunc.AggregateDistinctCount(it.Price)).ToList();
|
||||
Console.WriteLine("#### Examples End ####");
|
||||
}
|
||||
|
||||
private static void ReturnType()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### ReturnType Start ####");
|
||||
var db = GetInstance();
|
||||
List<Order> list = db.Queryable<Order>().ToList();
|
||||
|
||||
Order item = db.Queryable<Order>().First(it => it.Id == 1);
|
||||
|
||||
DataTable dataTable = db.Queryable<Order>().Select(it => it.Id).ToDataTable();
|
||||
|
||||
var json = db.Queryable<Order>().ToJson();
|
||||
|
||||
List<int> listInt = db.Queryable<Order>().Select(it => it.Id).ToList();
|
||||
|
||||
var dynamic = db.Queryable<Order>().Select<dynamic>().ToList();
|
||||
|
||||
var viewModel = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId ,
|
||||
JoinType.Left, o.CustomId == c.Id
|
||||
))
|
||||
.Select<ViewOrder>().ToList();
|
||||
|
||||
var newDynamic = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId,
|
||||
JoinType.Left, o.CustomId == c.Id
|
||||
))
|
||||
.Select((o, i, c) => new { orderName = o.Name, cusName=c.Name }).ToList();
|
||||
|
||||
var newClass = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId,
|
||||
JoinType.Left, o.CustomId == c.Id
|
||||
))
|
||||
.Select((o, i, c) => new ViewOrder { Name=o.Name, CustomName=c.Name }).ToList();
|
||||
|
||||
|
||||
var oneClass = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId,
|
||||
JoinType.Left, o.CustomId == c.Id
|
||||
))
|
||||
.Select((o, i, c) => c).ToList();
|
||||
|
||||
var twoClass = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId,
|
||||
JoinType.Left, o.CustomId == c.Id
|
||||
))
|
||||
.Select((o, i, c) => new { o,i}).ToList();
|
||||
|
||||
List<Dictionary<string, object>> ListDic = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId,
|
||||
JoinType.Left, o.CustomId == c.Id
|
||||
))
|
||||
.Select<ExpandoObject>().ToList().Select(it => it.ToDictionary(x => x.Key, x => x.Value)).ToList();
|
||||
Console.WriteLine("#### ReturnType End ####");
|
||||
}
|
||||
|
||||
private static void Subquery()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Subquery Start ####");
|
||||
var db = GetInstance();
|
||||
|
||||
var list = db.Queryable<Order>().Take(10).Select(it => new
|
||||
{
|
||||
customName=SqlFunc.Subqueryable<Custom>().Where("it.CustomId=id").Select(s=>s.Name),
|
||||
customName2 = SqlFunc.Subqueryable<Custom>().Where("it.CustomId = id").Where(s => true).Select(s => s.Name)
|
||||
}).ToList();
|
||||
|
||||
var list2 = db.Queryable<Order>().Where(it => SqlFunc.Subqueryable<OrderItem>().Where(i => i.OrderId == it.Id).Any()).ToList();
|
||||
|
||||
Console.WriteLine("#### Subquery End ####");
|
||||
}
|
||||
|
||||
private static void SqlFuncTest()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### SqlFunc Start ####");
|
||||
var db = GetInstance();
|
||||
var index= db.Queryable<Order>().Select(it => SqlFunc.Contains("a", "cccacc")).First();
|
||||
|
||||
Console.WriteLine("#### SqlFunc End ####");
|
||||
}
|
||||
|
||||
private static void Mapper()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Mapper Start ####");
|
||||
var db = GetInstance();
|
||||
//Creater Table
|
||||
db.CodeFirst.InitTables(typeof(Tree));
|
||||
db.DbMaintenance.TruncateTable("tree");
|
||||
db.Insertable(new Tree() { Id = 1, Name = "root" }).ExecuteCommand();
|
||||
db.Insertable(new Tree() { Id = 11, Name = "child1",ParentId=1 }).ExecuteCommand();
|
||||
db.Insertable(new Tree() { Id = 12, Name = "child2",ParentId=1 }).ExecuteCommand();
|
||||
db.Insertable(new Tree() { Id = 2, Name = "root" }).ExecuteCommand();
|
||||
db.Insertable(new Tree() { Id = 22, Name = "child3", ParentId = 2 }).ExecuteCommand();
|
||||
|
||||
// Same property name mapping,Both entities have parentId
|
||||
var list = db.Queryable<Tree>().Mapper(it => it.Parent, it => it.ParentId).ToList();
|
||||
|
||||
|
||||
//If both entities have parentId, I don't want to associate with parentId.
|
||||
var list1 =db.Queryable<Tree>()
|
||||
//parent=(select * from parent where id=it.parentid)
|
||||
.Mapper(it=>it.Parent,it=>it.ParentId, it=>it.Parent.Id)
|
||||
//Child=(select * from parent where ParentId=it.id)
|
||||
.Mapper(it => it.Child, it => it.Id, it => it.Parent.ParentId)
|
||||
.ToList();
|
||||
//one to one
|
||||
var list2 = db.Queryable<OrderItemInfo>().Mapper(it => it.Order, it => it.OrderId).ToList();
|
||||
|
||||
//one to many
|
||||
var list3 = db.Queryable<Order>().Mapper(it => it.Items, it => it.Items.First().OrderId).ToList();
|
||||
|
||||
//many to many
|
||||
db.CodeFirst.InitTables<A, B, ABMapping>();
|
||||
|
||||
db.Insertable(new A() { Name = "A" }).ExecuteCommand();
|
||||
db.Insertable(new B() { Name = "B" }).ExecuteCommand();
|
||||
db.Insertable(new ABMapping() { AId = 1, BId = 1 }).ExecuteCommand();
|
||||
|
||||
var list4 = db.Queryable<ABMapping>()
|
||||
.Mapper(it => it.A, it => it.AId)
|
||||
.Mapper(it => it.B, it => it.BId)
|
||||
.Where(it=>it.A.Id==1).ToList();
|
||||
|
||||
//Manual mode
|
||||
var result = db.Queryable<OrderInfo>().Take(10).Select<ViewOrder>().Mapper((itemModel, cache) =>
|
||||
{
|
||||
var allItems = cache.Get(orderList => {
|
||||
var allIds = orderList.Select(it => it.Id).ToList();
|
||||
return db.Queryable<OrderItem>().Where(it => allIds.Contains(it.OrderId)).ToList();//Execute only once
|
||||
});
|
||||
itemModel.Items = allItems.Where(it => it.OrderId==itemModel.Id).ToList();//Every time it's executed
|
||||
}).ToList();
|
||||
|
||||
Console.WriteLine("#### End Start ####");
|
||||
}
|
||||
|
||||
private static void NoEntity()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### No Entity Start ####");
|
||||
var db = GetInstance();
|
||||
|
||||
var list = db.Queryable<dynamic>().AS("order").Where("id=id", new { id = 1 }).ToList();
|
||||
|
||||
var list2 = db.Queryable<dynamic>("o").AS("order").AddJoinInfo("OrderDetail", "i", "o.id=i.OrderId").Where("id=id", new { id = 1 }).Select("o.*").ToList();
|
||||
Console.WriteLine("#### No Entity End ####");
|
||||
}
|
||||
|
||||
private static void JoinTable()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Join Table Start ####");
|
||||
var db = GetInstance();
|
||||
|
||||
//Simple join
|
||||
var list = db.Queryable<Order, OrderItem, Custom>((o, i, c) => o.Id == i.OrderId&&c.Id == o.CustomId)
|
||||
.Select<ViewOrder>()
|
||||
.ToList();
|
||||
|
||||
//Join table
|
||||
var list2 = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId,
|
||||
JoinType.Left, c.Id == o.CustomId
|
||||
))
|
||||
.Select<ViewOrder>().ToList();
|
||||
|
||||
//Join queryable
|
||||
var query1 = db.Queryable<Order, OrderItem>((o, i) => new JoinQueryInfos(
|
||||
JoinType.Left, o.Id == i.OrderId
|
||||
))
|
||||
.Where(o => o.Name == "jack");
|
||||
|
||||
var query2 = db.Queryable<Custom>();
|
||||
var list3=db.Queryable(query1, query2,JoinType.Left, (p1, p2) => p1.CustomId == p2.Id).Select<ViewOrder>().ToList();
|
||||
|
||||
Console.WriteLine("#### Join Table End ####");
|
||||
}
|
||||
|
||||
private static void QueryConditions()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Query Conditions Start ####");
|
||||
|
||||
SqlSugarClient db = GetInstance();
|
||||
|
||||
/*** By expression***/
|
||||
|
||||
//id=@id
|
||||
var list = db.Queryable<Order>().Where(it => it.Id == 1).ToList();
|
||||
//id=@id or name like '%'+@name+'%'
|
||||
var list2 = db.Queryable<Order>().Where(it => it.Id == 1 || it.Name.Contains("jack")).ToList();
|
||||
|
||||
|
||||
//Create expression
|
||||
var exp = Expressionable.Create<Order>()
|
||||
.And(it => it.Id == 1)
|
||||
.Or(it => it.Name.Contains("jack")).ToExpression();
|
||||
var list3 = db.Queryable<Order>().Where(exp).ToList();
|
||||
|
||||
|
||||
/*** By sql***/
|
||||
|
||||
//id=@id
|
||||
var list4 = db.Queryable<Order>().Where("id=@id", new { id = 1 }).ToList();
|
||||
//id=@id or name like '%'+@name+'%'
|
||||
var list5 = db.Queryable<Order>().Where("id=@id or name like @name ", new { id = 1, name = "%jack%" }).ToList();
|
||||
|
||||
|
||||
|
||||
/*** By dynamic***/
|
||||
|
||||
//id=1
|
||||
var conModels = new List<IConditionalModel>();
|
||||
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" , FieldValueConvertFunc=it=>Convert.ToInt32(it) });//id=1
|
||||
var student = db.Queryable<Order>().Where(conModels).ToList();
|
||||
|
||||
//Complex use case
|
||||
List<IConditionalModel> Order = new List<IConditionalModel>();
|
||||
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1", FieldValueConvertFunc = it => Convert.ToInt32(it) });//id=1
|
||||
//conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1", FieldValueConvertFunc = it => Convert.ToInt32(it) });// id like '%1%'
|
||||
//conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNullOrEmpty });
|
||||
//conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.In, FieldValue = "1,2,3" });
|
||||
//conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NotIn, FieldValue = "1,2,3" });
|
||||
//conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1,2,3" });
|
||||
//conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot, FieldValue = null });// id is not null
|
||||
|
||||
conModels.Add(new ConditionalCollections()
|
||||
{
|
||||
ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()// (id=1 or id=2 and id=1)
|
||||
{
|
||||
//new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
|
||||
new KeyValuePair<WhereType, ConditionalModel> (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" , FieldValueConvertFunc = it => Convert.ToInt32(it) }),
|
||||
new KeyValuePair<WhereType, ConditionalModel> ( WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" ,FieldValueConvertFunc = it => Convert.ToInt32(it)})
|
||||
}
|
||||
});
|
||||
var list6 = db.Queryable<Order>().Where(conModels).ToList();
|
||||
|
||||
/*** Conditional builder ***/
|
||||
|
||||
// use whereif
|
||||
string name = "";
|
||||
int id = 1;
|
||||
var query = db.Queryable<Order>()
|
||||
.WhereIF(!string.IsNullOrEmpty(name), it => it.Name.Contains(name))
|
||||
.WhereIF(id > 0, it => it.Id == id).ToList();
|
||||
//clone new Queryable
|
||||
var query2 = db.Queryable<Order>().Where(it => it.Id == 1);
|
||||
var list7 = query2.Clone().Where(it => it.Name == "jack").ToList();//id=1 and name = jack
|
||||
var list8 = query2.Clone().Where(it => it.Name == "tom").ToList();//id=1 and name = tom
|
||||
|
||||
Console.WriteLine("#### Condition Screening End ####");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void Async()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Async Start ####");
|
||||
|
||||
SqlSugarClient db = GetInstance();
|
||||
var task1 = db.Queryable<Order>().FirstAsync();
|
||||
task1.Wait();
|
||||
var task2 = db.Queryable<Order>().Where(it => it.Id == 1).ToListAsync();
|
||||
|
||||
|
||||
task2.Wait();
|
||||
|
||||
Console.WriteLine("#### Async End ####");
|
||||
}
|
||||
|
||||
private static SqlSugarClient GetInstance()
|
||||
{
|
||||
return new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = SqlSugar.DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
110
Src/Asp.NetCore2/HGTest/Demo/Demo2_Updateable.cs
Normal file
110
Src/Asp.NetCore2/HGTest/Demo/Demo2_Updateable.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo2_Updateable
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Updateable Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
/*** 1.entity or List ***/
|
||||
|
||||
var updateObj = new Order() { Id = 1, Name = "order1" };
|
||||
var updateObjs = new List<Order> {
|
||||
new Order() { Id = 11, Name = "order11" },
|
||||
new Order() { Id = 12, Name = "order12" }
|
||||
};
|
||||
|
||||
//update all columns by primary key
|
||||
var result = db.Updateable(updateObj).ExecuteCommand();//update single
|
||||
var result2 = db.Updateable(updateObjs).ExecuteCommand();//update List<Class>
|
||||
|
||||
//Ignore Name and Price
|
||||
var result3 = db.Updateable(updateObj).IgnoreColumns(it => new { it.CreateTime, it.Price }).ExecuteCommand();
|
||||
|
||||
//only update Name and CreateTime
|
||||
var result4 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name, it.CreateTime }).ExecuteCommand();
|
||||
|
||||
//If there is no primary key
|
||||
var result5 = db.Updateable(updateObj).WhereColumns(it => new { it.Id }).ExecuteCommand();//update single by id
|
||||
var result6 = db.Updateable(updateObjs).WhereColumns(it => new { it.Id }).ExecuteCommand();//update List<Class> by id
|
||||
|
||||
|
||||
|
||||
|
||||
/*** 2.by expression ***/
|
||||
|
||||
//update name,createtime
|
||||
var result7 = db.Updateable<Order>(it => new Order() { Name = "a", CreateTime = DateTime.Now }).Where(it => it.Id == 11).ExecuteCommand();
|
||||
var result71 = db.Updateable<Order>().SetColumns(it => new Order() { Name = "a", CreateTime = DateTime.Now }).Where(it => it.Id == 11).ExecuteCommand();
|
||||
//only update name
|
||||
var result8 = db.Updateable<Order>(it => it.Name == "Name").Where(it => it.Id == 1).ExecuteCommand();
|
||||
var result81 = db.Updateable<Order>().SetColumns(it => it.Name == "Name" ).Where(it => it.Id == 1).ExecuteCommand();
|
||||
//
|
||||
var result61 = db.Updateable<Order>()
|
||||
.InnerJoin<Custom>((x, y) => x.CustomId == y.Id)
|
||||
.SetColumns((x, y) => new Order() { Name = y.Name, Price = y.Id })
|
||||
.Where((x, y) => x.Id == 1)
|
||||
.ExecuteCommand();
|
||||
|
||||
|
||||
/*** 3.by Dictionary ***/
|
||||
var dt = new Dictionary<string, object>();
|
||||
dt.Add("id", 1);
|
||||
dt.Add("name", "abc");
|
||||
dt.Add("createTime", DateTime.Now);
|
||||
var dtList = new List<Dictionary<string, object>>();
|
||||
dtList.Add(dt);
|
||||
|
||||
var t66 = db.Updateable(dt).AS("order").WhereColumns("id").ExecuteCommand();
|
||||
var t666 = db.Updateable(dtList).AS("order").WhereColumns("id").ExecuteCommand();
|
||||
|
||||
|
||||
|
||||
/*** 4.Other instructions ***/
|
||||
|
||||
var caseValue = "1";
|
||||
//Do not update NULL columns
|
||||
db.Updateable(updateObj).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
|
||||
|
||||
//if 1 update name else if 2 update name,createtime
|
||||
db.Updateable(updateObj)
|
||||
.UpdateColumnsIF(caseValue == "1", it => new { it.Name })
|
||||
.UpdateColumnsIF(caseValue == "2", it => new { it.Name, it.CreateTime })
|
||||
.ExecuteCommand();
|
||||
//Use Lock
|
||||
db.Updateable(updateObj).With(SqlWith.UpdLock).ExecuteCommand();
|
||||
|
||||
//Where Sql
|
||||
//db.Updateable(updateObj).Where("id=@x", new { x = 1 }).ExecuteCommand();
|
||||
|
||||
Console.WriteLine("#### Updateable End ####");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
56
Src/Asp.NetCore2/HGTest/Demo/Demo3_Insertable.cs
Normal file
56
Src/Asp.NetCore2/HGTest/Demo/Demo3_Insertable.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo3_Insertable
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Insertable Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var insertObj = new Order() { Id = 1, Name = "order1",Price=0 };
|
||||
var updateObjs = new List<Order> {
|
||||
new Order() { Id = 11, Name = "order11", Price=0 },
|
||||
new Order() { Id = 12, Name = "order12" , Price=0}
|
||||
};
|
||||
|
||||
//Ignore CreateTime
|
||||
db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
|
||||
db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();
|
||||
|
||||
//Only insert Name and Price
|
||||
db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity();
|
||||
db.Insertable(insertObj).InsertColumns("Name", "Price").ExecuteReturnIdentity();
|
||||
|
||||
//ignore null columns
|
||||
db.Insertable(updateObjs).ExecuteCommand();//get change row count
|
||||
|
||||
//Use Lock
|
||||
db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
|
||||
|
||||
Console.WriteLine("#### Insertable End ####");
|
||||
}
|
||||
}
|
||||
}
|
48
Src/Asp.NetCore2/HGTest/Demo/Demo4_Deleteable.cs
Normal file
48
Src/Asp.NetCore2/HGTest/Demo/Demo4_Deleteable.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo4_Deleteable
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Deleteable Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
//by entity
|
||||
db.Deleteable<Order>().Where(new Order() { Id = 1111 }).ExecuteCommand();
|
||||
|
||||
//by primary key
|
||||
db.Deleteable<Order>().In(1111).ExecuteCommand();
|
||||
|
||||
//by primary key array
|
||||
db.Deleteable<Order>().In(new int[] { 1111, 2222 }).ExecuteCommand();
|
||||
|
||||
//by expression
|
||||
db.Deleteable<Order>().Where(it => it.Id == 11111).ExecuteCommand();
|
||||
|
||||
Console.WriteLine("#### Deleteable End ####");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
37
Src/Asp.NetCore2/HGTest/Demo/Demo5_SqlQueryable.cs
Normal file
37
Src/Asp.NetCore2/HGTest/Demo/Demo5_SqlQueryable.cs
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo5_SqlQueryable
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### SqlQueryable Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
|
||||
int total = 0;
|
||||
var list = db.SqlQueryable<Order>("select * from \"order\"").ToPageList(1, 2, ref total);
|
||||
|
||||
|
||||
//by expression
|
||||
var list2 = db.SqlQueryable<Order>("select * from \"order\"").Where(it => it.Id == 1).ToPageList(1, 2);
|
||||
//by sql
|
||||
var list3 = db.SqlQueryable<Order>("select * from \"order\"").Where("id=@id", new { id = 1 }).ToPageList(1, 2);
|
||||
|
||||
Console.WriteLine("#### SqlQueryable End ####");
|
||||
}
|
||||
}
|
||||
}
|
52
Src/Asp.NetCore2/HGTest/Demo/Demo6_Queue.cs
Normal file
52
Src/Asp.NetCore2/HGTest/Demo/Demo6_Queue.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo6_Queue
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Queue Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
db.Insertable<Order>(new Order() { Name = "a" }).AddQueue();
|
||||
db.Insertable<Order>(new Order() { Name = "b" }).AddQueue();
|
||||
db.SaveQueues();
|
||||
|
||||
|
||||
db.Insertable<Order>(new Order() { Name = "a" }).AddQueue();
|
||||
db.Insertable<Order>(new Order() { Name = "b" }).AddQueue();
|
||||
db.Insertable<Order>(new Order() { Name = "c" }).AddQueue();
|
||||
db.Insertable<Order>(new Order() { Name = "d" }).AddQueue();
|
||||
var ar = db.SaveQueuesAsync();
|
||||
ar.Wait();
|
||||
|
||||
db.Queryable<Order>().AddQueue();
|
||||
db.Queryable<Order>().AddQueue();
|
||||
db.AddQueue("select * from `order` where id=@id", new { id = 10000 });
|
||||
/* var result2 = db.SaveQueues<Order, Order, Order>()*/;
|
||||
|
||||
Console.WriteLine("#### Queue End ####");
|
||||
}
|
||||
}
|
||||
}
|
57
Src/Asp.NetCore2/HGTest/Demo/Demo7_Ado.cs
Normal file
57
Src/Asp.NetCore2/HGTest/Demo/Demo7_Ado.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo7_Ado
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Ado Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
//sql
|
||||
var dt = db.Ado.GetDataTable("select * from \"order\" where @id>0 or name=@name", new List<SugarParameter>(){
|
||||
new SugarParameter("@id",1),
|
||||
new SugarParameter("@name","2")
|
||||
});
|
||||
|
||||
//sql
|
||||
var dt2 = db.Ado.GetDataTable("select * from \"order\" where @id>0 or name=@name", new { id = 1, name = "2" });
|
||||
|
||||
//Stored Procedure
|
||||
//var dt3 = db.Ado.UseStoredProcedure().GetDataTable("sp_school", new { name = "张三", age = 0 });
|
||||
//var nameP = new SugarParameter("@name", "张三");
|
||||
//var ageP = new SugarParameter("@age", null, true);//isOutput=true
|
||||
//var dt4 = db.Ado.UseStoredProcedure().GetDataTable("sp_school", nameP, ageP);
|
||||
|
||||
|
||||
|
||||
//There are many methods to under db.ado
|
||||
var list= db.Ado.SqlQuery<Order>("select * from \"order\" ");
|
||||
var intValue=db.Ado.SqlQuerySingle<int>("select 1");
|
||||
db.Ado.ExecuteCommand("delete from \"order\" where id>1000");
|
||||
//db.Ado.xxx
|
||||
Console.WriteLine("#### Ado End ####");
|
||||
}
|
||||
}
|
||||
}
|
48
Src/Asp.NetCore2/HGTest/Demo/Demo8_Saveable.cs
Normal file
48
Src/Asp.NetCore2/HGTest/Demo/Demo8_Saveable.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo8_Saveable
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Saveable Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//insert or update
|
||||
db.Saveable<Order>(new Order() { Id=1, Name="jack" }).ExecuteReturnEntity();
|
||||
|
||||
|
||||
//insert or update
|
||||
db.Saveable<Order>(new Order() { Id = 1000, Name = "jack", CreateTime=DateTime.Now })
|
||||
.InsertColumns(it => new { it.Name,it.CreateTime, it.Price})//if insert into name,CreateTime,Price
|
||||
.UpdateColumns(it => new { it.Name, it.CreateTime })//if update set name CreateTime
|
||||
.ExecuteReturnEntity();
|
||||
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Saveable End ####");
|
||||
}
|
||||
}
|
||||
}
|
46
Src/Asp.NetCore2/HGTest/Demo/Demo9_EntityMain.cs
Normal file
46
Src/Asp.NetCore2/HGTest/Demo/Demo9_EntityMain.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo9_EntityMain
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### EntityMain Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
var entityInfo = db.EntityMaintenance.GetEntityInfo<Order>();
|
||||
foreach (var column in entityInfo.Columns)
|
||||
{
|
||||
Console.WriteLine(column.DbColumnName);
|
||||
}
|
||||
|
||||
var dbColumnsName = db.EntityMaintenance.GetDbColumnName<EntityMapper>("Name");
|
||||
|
||||
var dbTableName = db.EntityMaintenance.GetTableName<EntityMapper>();
|
||||
|
||||
//more https://github.com/sunkaixuan/SqlSugar/wiki/9.EntityMain
|
||||
Console.WriteLine("#### EntityMain End ####");
|
||||
}
|
||||
}
|
||||
}
|
42
Src/Asp.NetCore2/HGTest/Demo/DemoA_DbMain.cs
Normal file
42
Src/Asp.NetCore2/HGTest/Demo/DemoA_DbMain.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoA_DbMain
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### DbMain Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var tables = db.DbMaintenance.GetTableInfoList();
|
||||
foreach (var table in tables)
|
||||
{
|
||||
Console.WriteLine(table.Description);
|
||||
}
|
||||
//more https://github.com/sunkaixuan/SqlSugar/wiki/a.DbMain
|
||||
Console.WriteLine("#### DbMain End ####");
|
||||
}
|
||||
}
|
||||
}
|
68
Src/Asp.NetCore2/HGTest/Demo/DemoB_Aop.cs
Normal file
68
Src/Asp.NetCore2/HGTest/Demo/DemoB_Aop.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoB_Aop
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Aop Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
db.Aop.OnLogExecuted = (sql, pars) => //SQL executed event
|
||||
{
|
||||
Console.WriteLine("OnLogExecuted"+sql);
|
||||
};
|
||||
db.Aop.OnLogExecuting = (sql, pars) => //SQL executing event (pre-execution)
|
||||
{
|
||||
Console.WriteLine("OnLogExecuting" + sql);
|
||||
};
|
||||
db.Aop.OnError = (exp) =>//SQL execution error event
|
||||
{
|
||||
//exp.sql
|
||||
};
|
||||
db.Aop.OnExecutingChangeSql = (sql, pars) => //SQL executing event (pre-execution,SQL script can be modified)
|
||||
{
|
||||
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
||||
};
|
||||
db.Aop.OnDiffLogEvent = it =>//Get data changes
|
||||
{
|
||||
var editBeforeData = it.BeforeData;
|
||||
var editAfterData = it.AfterData;
|
||||
var sql = it.Sql;
|
||||
var parameter = it.Parameters;
|
||||
var businessData = it.BusinessData;
|
||||
var time = it.Time;
|
||||
var diffType = it.DiffType;//enum insert 、update and delete
|
||||
Console.WriteLine(businessData);
|
||||
Console.WriteLine(editBeforeData[0].Columns[1].Value);
|
||||
Console.WriteLine("to");
|
||||
Console.WriteLine(editAfterData[0].Columns[1].Value);
|
||||
//Write logic
|
||||
};
|
||||
|
||||
|
||||
db.Queryable<Order>().ToList();
|
||||
db.Queryable<OrderItem>().ToList();
|
||||
|
||||
//OnDiffLogEvent
|
||||
var data = db.Queryable<Order>().First();
|
||||
data.Name = "changeName";
|
||||
db.Updateable(data).EnableDiffLogEvent("--update Order--").ExecuteCommand();
|
||||
|
||||
Console.WriteLine("#### Aop End ####");
|
||||
}
|
||||
}
|
||||
}
|
75
Src/Asp.NetCore2/HGTest/Demo/DemoD_DbFirst.cs
Normal file
75
Src/Asp.NetCore2/HGTest/Demo/DemoD_DbFirst.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoD_DbFirst
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("#### DbFirst Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
|
||||
db.DbFirst.CreateClassFile("c:\\Demo\\1", "Models");
|
||||
|
||||
|
||||
db.DbFirst.Where("Student").CreateClassFile("c:\\Demo\\2", "Models");
|
||||
|
||||
|
||||
db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\3", "Models");
|
||||
|
||||
|
||||
db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\4", "Models");
|
||||
|
||||
|
||||
db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\5", "Models");
|
||||
|
||||
|
||||
db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\6", "Demo.Models");
|
||||
|
||||
|
||||
db.DbFirst. SettingClassTemplate(old => { return old;})
|
||||
.SettingNamespaceTemplate(old =>{ return old;})
|
||||
.SettingPropertyDescriptionTemplate(old =>
|
||||
{
|
||||
return @" /// <summary>
|
||||
/// Desc_New:{PropertyDescription}
|
||||
/// Default_New:{DefaultValue}
|
||||
/// Nullable_New:{IsNullable}
|
||||
/// </summary>";
|
||||
})
|
||||
.SettingPropertyTemplate(old =>{return old;})
|
||||
.SettingConstructorTemplate(old =>{return old; })
|
||||
.CreateClassFile("c:\\Demo\\7");
|
||||
|
||||
|
||||
|
||||
foreach (var item in db.DbMaintenance.GetTableInfoList())
|
||||
{
|
||||
string entityName = item.Name.ToUpper();/*Format class name*/
|
||||
db.MappingTables.Add(entityName , item.Name);
|
||||
foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
|
||||
{
|
||||
db.MappingColumns.Add(col.DbColumnName.ToUpper() /*Format class property name*/, col.DbColumnName, entityName);
|
||||
}
|
||||
}
|
||||
db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\8", "Models");
|
||||
|
||||
|
||||
//Use Razor Template
|
||||
//db.DbFirst.UseRazorAnalysis(RazorFirst.DefaultRazorClassTemplate).CreateClassFile("");
|
||||
|
||||
Console.WriteLine("#### DbFirst End ####");
|
||||
}
|
||||
}
|
||||
}
|
47
Src/Asp.NetCore2/HGTest/Demo/DemoE_CodeFirst.cs
Normal file
47
Src/Asp.NetCore2/HGTest/Demo/DemoE_CodeFirst.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoE_CodeFirst
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### CodeFirst Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString3,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
|
||||
db.Insertable(new CodeFirstTable1() { Name = "a", Text="a" }).ExecuteCommand();
|
||||
var list = db.Queryable<CodeFirstTable1>().ToList();
|
||||
db.CodeFirst.InitTables<CodeFirstChar2>();
|
||||
db.Insertable(new CodeFirstChar2() { CharTest = '1' }).ExecuteCommand();
|
||||
var list2=db.Queryable<CodeFirstChar2>().ToList();
|
||||
Console.WriteLine("#### CodeFirst end ####");
|
||||
}
|
||||
}
|
||||
public class CodeFirstChar2
|
||||
{
|
||||
[SqlSugar.SugarColumn(ColumnDataType ="varchar(1)")]
|
||||
public char CharTest { get; set; }
|
||||
}
|
||||
public class CodeFirstTable1
|
||||
{
|
||||
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
[SugarColumn(ColumnDataType = "varchar(255)")]//custom
|
||||
public string Text { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
46
Src/Asp.NetCore2/HGTest/Demo/DemoF_Utilities.cs
Normal file
46
Src/Asp.NetCore2/HGTest/Demo/DemoF_Utilities.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoF_Utilities
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Utilities Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
List<int> ids = Enumerable.Range(1, 100).ToList();
|
||||
db.Utilities.PageEach(ids, 10, list =>
|
||||
{
|
||||
Console.WriteLine(string.Join("," ,list));
|
||||
});
|
||||
|
||||
var list2= db.Utilities.DataTableToList<Order>(db.Ado.GetDataTable("select * from \"order\""));
|
||||
|
||||
//more https://github.com/sunkaixuan/SqlSugar/wiki/f.Utilities
|
||||
Console.WriteLine("#### Utilities End ####");
|
||||
}
|
||||
}
|
||||
}
|
36
Src/Asp.NetCore2/HGTest/Demo/DemoG_SimpleClient.cs
Normal file
36
Src/Asp.NetCore2/HGTest/Demo/DemoG_SimpleClient.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoG_SimpleClient
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### SimpleClient Start ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Console.WriteLine("#### SimpleClient End ####");
|
||||
}
|
||||
}
|
||||
}
|
77
Src/Asp.NetCore2/HGTest/Demo/Democ_GobalFilter.cs
Normal file
77
Src/Asp.NetCore2/HGTest/Demo/Democ_GobalFilter.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoC_GobalFilter
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Filter Start ####");
|
||||
var db = GetInstance();
|
||||
|
||||
|
||||
var sql = db.Queryable<Order>().ToSql();
|
||||
//SELECT [Id],[Name],[Price],[CreateTime] FROM `order` WHERE isDelete=0
|
||||
Console.WriteLine(sql);
|
||||
|
||||
|
||||
var sql2 = db.Queryable<Order,OrderItem>((main,ot)=> main.Id==ot.OrderId).ToSql();
|
||||
//SELECT [Id],[Name],[Price],[CreateTime] FROM `order` main ,[OrderDetail] ot WHERE ( [main].[Id] = [ot].[OrderId] ) AND main.isDelete=0
|
||||
Console.WriteLine(sql2);
|
||||
|
||||
|
||||
var sql3 = db.Queryable<Order>().Filter("Myfilter").ToSql();// Myfilter+Gobal
|
||||
//SELECT [Id],[Name],[Price],[CreateTime] FROM `order` WHERE Name='jack' AND isDelete=0
|
||||
Console.WriteLine(sql3);
|
||||
|
||||
var sql4 = db.Queryable<Order>().Filter("Myfilter",isDisabledGobalFilter:true).ToSql();//only Myfilter
|
||||
//SELECT [Id],[Name],[Price],[CreateTime] FROM `order` WHERE Name='jack'
|
||||
Console.WriteLine(sql4);
|
||||
Console.WriteLine("#### Filter End ####");
|
||||
}
|
||||
|
||||
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.HG, ConnectionString = Config.ConnectionString, IsAutoCloseConnection = true });
|
||||
|
||||
//single table query gobal filter
|
||||
db.QueryFilter.Add(new SqlFilterItem()
|
||||
{
|
||||
FilterValue = filterDb =>
|
||||
{
|
||||
//Writable logic
|
||||
return new SqlFilterResult() { Sql = " isDelete=0" };//Global string perform best
|
||||
}
|
||||
});
|
||||
|
||||
//Multi-table query gobal filter
|
||||
db.QueryFilter.Add(new SqlFilterItem()
|
||||
{
|
||||
FilterValue = filterDb =>
|
||||
{
|
||||
//Writable logic
|
||||
return new SqlFilterResult() { Sql = " main.isDelete=0" };
|
||||
},
|
||||
IsJoinQuery=true
|
||||
});
|
||||
|
||||
//Specific filters
|
||||
db.QueryFilter.Add(new SqlFilterItem()
|
||||
{
|
||||
FilterName= "Myfilter",
|
||||
FilterValue = filterDb =>
|
||||
{
|
||||
//Writable logic
|
||||
return new SqlFilterResult() { Sql = "Name='jack'" };
|
||||
}
|
||||
});
|
||||
return db;
|
||||
}
|
||||
}
|
||||
}
|
13
Src/Asp.NetCore2/HGTest/HGTest.csproj
Normal file
13
Src/Asp.NetCore2/HGTest/HGTest.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SqlSugar.HG\SqlSugar.HGCore.csproj" />
|
||||
<ProjectReference Include="..\SqlSugar\SqlSugar.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
20
Src/Asp.NetCore2/HGTest/Models/AttributeTable.cs
Normal file
20
Src/Asp.NetCore2/HGTest/Models/AttributeTable.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
[Table("MyAttributeTable")]
|
||||
//[SugarTable("CustomAttributeTable")]
|
||||
public class AttributeTable
|
||||
{
|
||||
|
||||
[Key]
|
||||
//[SugarColumn(IsPrimaryKey =true)]
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
7
Src/Asp.NetCore2/HGTest/Models/CarType.cs
Normal file
7
Src/Asp.NetCore2/HGTest/Models/CarType.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace OrmTest
|
||||
{
|
||||
public class CarType
|
||||
{
|
||||
public bool State { get; set; }
|
||||
}
|
||||
}
|
14
Src/Asp.NetCore2/HGTest/Models/Custom.cs
Normal file
14
Src/Asp.NetCore2/HGTest/Models/Custom.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Custom
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
15
Src/Asp.NetCore2/HGTest/Models/EntityMapper.cs
Normal file
15
Src/Asp.NetCore2/HGTest/Models/EntityMapper.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
namespace OrmTest
|
||||
{
|
||||
[SugarTable("MyEntityMapper")]
|
||||
public class EntityMapper
|
||||
{
|
||||
[SugarColumn(ColumnName ="MyName")]
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
54
Src/Asp.NetCore2/HGTest/Models/Mapper.cs
Normal file
54
Src/Asp.NetCore2/HGTest/Models/Mapper.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
[SugarTable("OrderDetail")]
|
||||
public class OrderItemInfo
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int ItemId { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public decimal? Price { get; set; }
|
||||
[SqlSugar.SugarColumn(IsNullable = true)]
|
||||
public DateTime? CreateTime { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public Order Order { get; set; }
|
||||
}
|
||||
[SugarTable("Order")]
|
||||
public class OrderInfo
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public List<OrderItem> Items { get; set; }
|
||||
}
|
||||
public class ABMapping
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int AId { get; set; }
|
||||
public int BId { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public A A { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public B B { get; set; }
|
||||
|
||||
}
|
||||
public class A
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
public class B
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
20
Src/Asp.NetCore2/HGTest/Models/MyCustomAttributeTable.cs
Normal file
20
Src/Asp.NetCore2/HGTest/Models/MyCustomAttributeTable.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
[Table("CustomAttributeTable")]
|
||||
//[SugarTable("CustomAttributeTable")]
|
||||
public class MyCustomAttributeTable
|
||||
{
|
||||
|
||||
[Key]
|
||||
//[SugarColumn(IsPrimaryKey =true)]
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
24
Src/Asp.NetCore2/HGTest/Models/Order.cs
Normal file
24
Src/Asp.NetCore2/HGTest/Models/Order.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
|
||||
public class Order
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
[SugarColumn(IsNullable =true)]
|
||||
public int CustomId { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public List<OrderItem> Items { get; set; }
|
||||
}
|
||||
}
|
18
Src/Asp.NetCore2/HGTest/Models/OrderItem.cs
Normal file
18
Src/Asp.NetCore2/HGTest/Models/OrderItem.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
[SqlSugar.SugarTable("OrderDetail")]
|
||||
public class OrderItem
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true)]
|
||||
public int ItemId { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public decimal? Price { get; set; }
|
||||
[SqlSugar.SugarColumn(IsNullable = true)]
|
||||
public DateTime? CreateTime { get; set; }
|
||||
}
|
||||
}
|
17
Src/Asp.NetCore2/HGTest/Models/TestTree.cs
Normal file
17
Src/Asp.NetCore2/HGTest/Models/TestTree.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class TestTree
|
||||
{
|
||||
[SqlSugar.SugarColumn(ColumnDataType = "hierarchyid")]
|
||||
public string TreeId { get; set; }
|
||||
[SqlSugar.SugarColumn(ColumnDataType = "Geography")]
|
||||
public string GId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
20
Src/Asp.NetCore2/HGTest/Models/Tree.cs
Normal file
20
Src/Asp.NetCore2/HGTest/Models/Tree.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Tree
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int ParentId { get; set; }
|
||||
[SqlSugar.SugarColumn(IsIgnore = true)]
|
||||
public Tree Parent { get; set; }
|
||||
[SqlSugar.SugarColumn(IsIgnore = true)]
|
||||
public List<Tree> Child { get; set; }
|
||||
}
|
||||
}
|
13
Src/Asp.NetCore2/HGTest/Models/ViewOrder.cs
Normal file
13
Src/Asp.NetCore2/HGTest/Models/ViewOrder.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class ViewOrder:Order
|
||||
{
|
||||
public string CustomName { get; set; }
|
||||
}
|
||||
}
|
40
Src/Asp.NetCore2/HGTest/Program.cs
Normal file
40
Src/Asp.NetCore2/HGTest/Program.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//Demo
|
||||
Demo0_SqlSugarClient.Init();
|
||||
Demo1_Queryable.Init();
|
||||
Demo2_Updateable.Init();
|
||||
Demo3_Insertable.Init();
|
||||
Demo4_Deleteable.Init();
|
||||
Demo5_SqlQueryable.Init();
|
||||
Demo6_Queue.Init();
|
||||
Demo7_Ado.Init();
|
||||
Demo8_Saveable.Init();
|
||||
Demo9_EntityMain.Init();
|
||||
DemoA_DbMain.Init();
|
||||
DemoB_Aop.Init();
|
||||
DemoC_GobalFilter.Init();
|
||||
DemoD_DbFirst.Init(); ;
|
||||
DemoE_CodeFirst.Init();
|
||||
DemoF_Utilities.Init();
|
||||
DemoG_SimpleClient.Init();
|
||||
|
||||
//Unit test
|
||||
//NewUnitTest.Init();
|
||||
|
||||
//Rest Data
|
||||
NewUnitTest.RestData();
|
||||
|
||||
Console.WriteLine("all successfully.");
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
48
Src/Asp.NetCore2/HGTest/UnitTest/Main.cs
Normal file
48
Src/Asp.NetCore2/HGTest/UnitTest/Main.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static SqlSugarClient Db=> new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public static void RestData()
|
||||
{
|
||||
Db.DbMaintenance.TruncateTable<Order>();
|
||||
Db.DbMaintenance.TruncateTable<OrderItem>();
|
||||
}
|
||||
public static void Init()
|
||||
{
|
||||
UnitSubToList.Init();
|
||||
Unit001.Init();
|
||||
Bulk();
|
||||
CodeFirst();
|
||||
Updateable();
|
||||
Json();
|
||||
Ado();
|
||||
Queryable();
|
||||
QueryableAsync();
|
||||
Thread();
|
||||
Thread2();
|
||||
Thread3();
|
||||
}
|
||||
}
|
||||
}
|
57
Src/Asp.NetCore2/HGTest/UnitTest/UAdo.cs
Normal file
57
Src/Asp.NetCore2/HGTest/UnitTest/UAdo.cs
Normal file
@ -0,0 +1,57 @@
|
||||
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 Ado()
|
||||
{
|
||||
|
||||
var task1 = Db.Ado.GetScalarAsync("select 1");
|
||||
task1.Wait();
|
||||
UValidate.Check(1, task1.Result, "ado");
|
||||
|
||||
var task2 = Db.Ado.GetIntAsync("select 2");
|
||||
task2.Wait();
|
||||
UValidate.Check(2, task2.Result, "ado");
|
||||
|
||||
|
||||
var task3 = Db.Ado.GetLongAsync("select 3");
|
||||
task3.Wait();
|
||||
UValidate.Check(3, task3.Result, "ado");
|
||||
|
||||
|
||||
var task4 = Db.Ado.GetDataTableAsync("select 4 as id");
|
||||
task4.Wait();
|
||||
UValidate.Check(4, task4.Result.Rows[0]["id"], "ado");
|
||||
|
||||
|
||||
var task5 = Db.Ado.GetInt("select @id as id",new { id=5});
|
||||
UValidate.Check(5, task5, "ado");
|
||||
|
||||
|
||||
|
||||
var task6 = Db.Ado.SqlQuery<dynamic>("select @id as id", new { id = 5 });
|
||||
UValidate.Check(5, task6[0].id, "ado");
|
||||
|
||||
|
||||
var task7 = Db.Ado.SqlQueryAsync<dynamic>("select @id as id", new { id = 7 });
|
||||
task7.Wait();
|
||||
UValidate.Check(7, task7.Result[0].id, "ado");
|
||||
|
||||
|
||||
var task8 = Db.Ado.SqlQueryAsync<dynamic>("select 8 as id");
|
||||
task8.Wait();
|
||||
UValidate.Check(8, task8.Result[0].id, "ado");
|
||||
|
||||
var task9=Db.Ado.SqlQuery<Order, OrderItem>(@"select * from ""order"";select * from OrderDetail");
|
||||
|
||||
var task10 = Db.Ado.SqlQueryAsync<Order, OrderItem>(@"select * from ""order"";select * from OrderDetail");
|
||||
task10.Wait();
|
||||
}
|
||||
}
|
||||
}
|
129
Src/Asp.NetCore2/HGTest/UnitTest/UBulkCopy.cs
Normal file
129
Src/Asp.NetCore2/HGTest/UnitTest/UBulkCopy.cs
Normal file
@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static void Bulk()
|
||||
{
|
||||
Db.CodeFirst.InitTables<UnitIdentity1>();
|
||||
Db.DbMaintenance.TruncateTable<UnitIdentity1>();
|
||||
var data1 = new UnitIdentity1()
|
||||
{
|
||||
Name = "a",
|
||||
Id=1
|
||||
};
|
||||
var data2 = new UnitIdentity1()
|
||||
{
|
||||
Name = "b",
|
||||
Id = 2
|
||||
};
|
||||
var data3 = new UnitIdentity1()
|
||||
{
|
||||
Name = "c",
|
||||
Id = 3
|
||||
};
|
||||
Db.Fastest<UnitIdentity1>().BulkCopy(new List<UnitIdentity1>() {
|
||||
data1
|
||||
});
|
||||
var list=Db.Queryable<UnitIdentity1>().ToList();
|
||||
if (list.Count != 1 || data1.Name != list.First().Name)
|
||||
{
|
||||
throw new Exception("unit Bulk");
|
||||
}
|
||||
|
||||
Db.Fastest<UnitIdentity1>().BulkCopy(new List<UnitIdentity1>() {
|
||||
data2,
|
||||
data3
|
||||
});
|
||||
list = Db.Queryable<UnitIdentity1>().ToList();
|
||||
if (list.Count != 3 || !list.Any(it=>it.Name=="c"))
|
||||
{
|
||||
throw new Exception("unit Bulk");
|
||||
}
|
||||
Db.Fastest<UnitIdentity1>().BulkUpdate(new List<UnitIdentity1>() {
|
||||
new UnitIdentity1(){
|
||||
Id=1,
|
||||
Name="222"
|
||||
},
|
||||
new UnitIdentity1(){
|
||||
Id=2,
|
||||
Name="111"
|
||||
}
|
||||
});
|
||||
list = Db.Queryable<UnitIdentity1>().ToList();
|
||||
if (list.First(it=>it.Id==1).Name!="222")
|
||||
{
|
||||
throw new Exception("unit Bulk");
|
||||
}
|
||||
if (list.First(it => it.Id == 2).Name != "111")
|
||||
{
|
||||
throw new Exception("unit Bulk");
|
||||
}
|
||||
if (list.First(it => it.Id == 3).Name != "c")
|
||||
{
|
||||
throw new Exception("unit Bulk");
|
||||
}
|
||||
Db.CodeFirst.InitTables<UnitIdentity111>();
|
||||
Db.DbMaintenance.TruncateTable<UnitIdentity111>();
|
||||
var count = Db.Fastest<UnitIdentity111111111>().AS("UnitIdentity111").BulkCopy(new List<UnitIdentity111111111> {
|
||||
new UnitIdentity111111111(){ Id=1, Name="jack" }
|
||||
});
|
||||
if (count == 0)
|
||||
{
|
||||
throw new Exception("unit Bulk");
|
||||
}
|
||||
count = Db.Fastest<UnitIdentity111111111>().AS("UnitIdentity111").BulkUpdate(new List<UnitIdentity111111111> {
|
||||
new UnitIdentity111111111(){ Id=1, Name="jack" }
|
||||
});
|
||||
if (count == 0)
|
||||
{
|
||||
throw new Exception("unit Bulk");
|
||||
}
|
||||
Db.CodeFirst.InitTables<UnitTable001>();
|
||||
Db.Fastest<UnitTable001>().BulkUpdate(new List<UnitTable001> {
|
||||
new UnitTable001(){ Id=1, table="a" }
|
||||
});
|
||||
Db.CodeFirst.InitTables<UnitTable01231101>();
|
||||
Db.Fastest<UnitTable01231101>().BulkCopy(new List<UnitTable01231101>() { new UnitTable01231101() {
|
||||
table=9.4E-05,
|
||||
Id=new Random().Next(1,9999999)
|
||||
} });
|
||||
var list2=Db.Queryable<UnitTable01231101>().ToList();
|
||||
}
|
||||
}
|
||||
public class UnitTable01231101
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
public double table { get; set; }
|
||||
}
|
||||
|
||||
public class UnitTable001
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
public string table { get; set; }
|
||||
}
|
||||
|
||||
public class UnitIdentity111
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
public class UnitIdentity111111111
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
public class UnitIdentity1
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
94
Src/Asp.NetCore2/HGTest/UnitTest/UCodeFirst.cs
Normal file
94
Src/Asp.NetCore2/HGTest/UnitTest/UCodeFirst.cs
Normal file
@ -0,0 +1,94 @@
|
||||
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 CodeFirst()
|
||||
{
|
||||
if (Db.DbMaintenance.IsAnyTable("UnitCodeTest1", false))
|
||||
Db.DbMaintenance.DropTable("UnitCodeTest1");
|
||||
Db.CodeFirst.InitTables<UnitCodeTest1>();
|
||||
Db.CodeFirst.InitTables<UnitIndextest>();
|
||||
var db = Db;
|
||||
db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
PgSqlIsAutoToLower = false,
|
||||
PgSqlIsAutoToLowerCodeFirst = false
|
||||
};
|
||||
db.CodeFirst.InitTables<UpperOrder, UpperItem>();
|
||||
var list = db.Queryable<UpperOrder>().LeftJoin<UpperOrder>((X1, Y1) =>
|
||||
X1.Id == Y1.Id)
|
||||
.Where(X1 => X1.Id == 1)
|
||||
.Select(X1 => new {
|
||||
x1 = X1.Id,
|
||||
x2 = X1.Name
|
||||
}).ToList();
|
||||
|
||||
var list2 = db.Queryable<UpperOrder>().LeftJoin<UpperItem>((X1, Y1) =>
|
||||
X1.Id == Y1.Id)
|
||||
.Where(X1 => X1.Id == 1)
|
||||
.Select<VUpperOrder>().ToList();
|
||||
}
|
||||
|
||||
public class VUpperOrder
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
|
||||
public DateTime UpperItemCreateTime { get; set; }
|
||||
public int UpperItemCustomId { get; set; }
|
||||
|
||||
}
|
||||
public class UpperOrder
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int CustomId { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public List<OrderItem> Items { get; set; }
|
||||
}
|
||||
|
||||
public class UpperItem
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime CreateTime { get; set; }
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int CustomId { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public List<OrderItem> Items { get; set; }
|
||||
}
|
||||
[SqlSugar.SugarIndex("UnitIndextestIndex", nameof(UnitIndextest.Table), SqlSugar.OrderByType.Asc)]
|
||||
public class UnitIndextest
|
||||
{
|
||||
public string Table { get; set; }
|
||||
public string Id { get; set; }
|
||||
}
|
||||
public class UnitCodeTest1
|
||||
{
|
||||
[SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "group1" })]
|
||||
public int Id { get; set; }
|
||||
[SqlSugar.SugarColumn(DefaultValue= "now()", IndexGroupNameList =new string[] {"group1" } )]
|
||||
public DateTime? CreateDate { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
44
Src/Asp.NetCore2/HGTest/UnitTest/UJson.cs
Normal file
44
Src/Asp.NetCore2/HGTest/UnitTest/UJson.cs
Normal file
@ -0,0 +1,44 @@
|
||||
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 Json()
|
||||
{
|
||||
Db.CodeFirst.InitTables<UnitJsonTest>();
|
||||
Db.DbMaintenance.TruncateTable<UnitJsonTest>();
|
||||
Db.Insertable(new UnitJsonTest() { Order = new Order { Id = 1, Name = "order1" } }).ExecuteCommand();
|
||||
var list = Db.Queryable<UnitJsonTest>().ToList();
|
||||
UValidate.Check("order1", list.First().Order.Name, "Json");
|
||||
Db.Updateable(new UnitJsonTest() { Id = Db.Queryable<UnitJsonTest>().First().Id, Order = new Order { Id = 2, Name = "order2" } }).ExecuteCommand();
|
||||
list= Db.Queryable<UnitJsonTest>().ToList();
|
||||
UValidate.Check("order2", list.First().Order.Name, "Json");
|
||||
var list2 = Db.Queryable<UnitJsonTest>().ToList();
|
||||
Db.CodeFirst.InitTables<UnitArrayTest>();
|
||||
Db.Updateable(new UnitArrayTest() { Name="a" }).IgnoreColumns(true).ExecuteCommand();
|
||||
}
|
||||
}
|
||||
|
||||
public class UnitArrayTest
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set;}
|
||||
[SqlSugar.SugarColumn(IsArray =true)]
|
||||
public int[] ids { get; set; }
|
||||
}
|
||||
|
||||
public class UnitJsonTest
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
[SqlSugar.SugarColumn(ColumnDataType = "varchar(4000)", IsJson = true)]
|
||||
public Order Order { get; set; }
|
||||
}
|
||||
}
|
323
Src/Asp.NetCore2/HGTest/UnitTest/UQueryable.cs
Normal file
323
Src/Asp.NetCore2/HGTest/UnitTest/UQueryable.cs
Normal file
@ -0,0 +1,323 @@
|
||||
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 Queryable()
|
||||
{
|
||||
|
||||
var pageindex = 1;
|
||||
var pagesize = 10;
|
||||
var total = 0;
|
||||
var totalPage = 0;
|
||||
var list = Db.Queryable<Order>().ToPageList(pageindex, pagesize, ref total, ref totalPage);
|
||||
|
||||
//Db.CodeFirst.InitTables(typeof(CarType));
|
||||
//Db.Updateable<CarType>()
|
||||
// .SetColumns(it => new CarType { State = SqlSugar.SqlFunc.IIF(it.State == true, false, true) }).Where(it => true)
|
||||
// .ExecuteCommand();
|
||||
|
||||
//Db.CodeFirst.InitTables(typeof(TestTree));
|
||||
//Db.DbMaintenance.TruncateTable<TestTree>();
|
||||
//Db.Ado.ExecuteCommand("insert testtree values(hierarchyid::GetRoot(),geography :: STGeomFromText ('POINT(55.9271035250276 -3.29431266523898)',4326),'name')");
|
||||
//var list2 = Db.Queryable<TestTree>().ToList();
|
||||
|
||||
Db.CodeFirst.InitTables<UnitGuidTable>();
|
||||
Db.Queryable<UnitGuidTable>().Where(it => it.Id.HasValue).ToList();
|
||||
|
||||
Db.Queryable<Order>().Where(it => SqlSugar.SqlFunc.Equals(it.CreateTime.Date, it.CreateTime.Date)).ToList();
|
||||
|
||||
var sql = Db.Queryable<UnitSelectTest>().Select(it => new UnitSelectTest()
|
||||
{
|
||||
|
||||
DcNull = it.Dc,
|
||||
Dc = it.Int
|
||||
}).ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT \"dc\" AS \"dcnull\" , \"int\" AS \"dc\" FROM \"unitselecttest\"", "Queryable");
|
||||
|
||||
sql = Db.Updateable<UnitSelectTest2>(new UnitSelectTest2()).ToSql().Key;
|
||||
UValidate.Check(sql, @"UPDATE ""unitselecttest2"" SET
|
||||
""dc""=@Dc,""intnull""=@IntNull WHERE ""int""=@Int", "Queryable");
|
||||
|
||||
sql = Db.Queryable<Order>().IgnoreColumns(it => it.CreateTime).ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT \"id\",\"name\",\"price\",\"customid\" FROM \"order\" ", "Queryable");
|
||||
sql = Db.Queryable<Order>().IgnoreColumns(it => new { it.Id, it.Name }).ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT \"price\",\"createtime\",\"customid\" FROM \"order\" ", "Queryable");
|
||||
sql = Db.Queryable<Order>().IgnoreColumns("id").ToSql().Key;
|
||||
UValidate.Check(sql, "SELECT \"name\",\"price\",\"createtime\",\"customid\" FROM \"order\" ", "Queryable");
|
||||
|
||||
var cts = IEnumerbleContains.Data();
|
||||
var list2=Db.Queryable<Order>()
|
||||
.Where(p => /*ids.*/cts.Select(c => c.Id).Contains(p.Id)).ToList();
|
||||
|
||||
var cts2 = IEnumerbleContains.Data().ToList(); ;
|
||||
var list3 = Db.Queryable<Order>()
|
||||
.Where(p => /*ids.*/cts2.Select(c => c.Id).Contains(p.Id)).ToList();
|
||||
|
||||
|
||||
var list4 = Db.Queryable<Order>()
|
||||
.Where(p => new List<int> { 1, 2, 3 }.Where(b => b > 1).Contains(p.Id)).ToList();
|
||||
|
||||
Db.CodeFirst.InitTables<UnitTest3>();
|
||||
var list5 = Db.Queryable<UnitTest3>().Where(it => SqlSugar.SqlFunc.ToString(it.Date.Value.Year) == "1").ToList();
|
||||
var list6 = Db.Queryable<UnitTest3>().Where(it => it.Date.Value.Year == 1).ToList();
|
||||
var list7 = Db.Queryable<UnitTest3>().Where(it => it.Date.Value.Date == DateTime.Now.Date).ToList();
|
||||
|
||||
|
||||
SaleOrder saleOrderInfo = new SaleOrder();
|
||||
Db.CodeFirst.InitTables<SaleOrder>();
|
||||
var result = Db.GetSimpleClient<SaleOrder>().Update(o => new SaleOrder()
|
||||
{
|
||||
OrderStatus = 1,
|
||||
CheckMan = saleOrderInfo.CheckMan,
|
||||
CheckTime = DateTime.Now
|
||||
}, o => o.OrderSn == saleOrderInfo.OrderSn && o.OrderStatus != 1);
|
||||
}
|
||||
|
||||
public static class IEnumerbleContains
|
||||
{
|
||||
public static IEnumerable<Order> Data()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
yield return new Order
|
||||
{
|
||||
Id = i,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
[SugarTable("UnitSaleOrder")]
|
||||
public class SaleOrder
|
||||
{
|
||||
public SaleOrder()
|
||||
{
|
||||
SaleDate = DateTime.Now;
|
||||
Team = 1;
|
||||
AddTime = DateTime.Now;
|
||||
OrderStatus = 0;
|
||||
Points = 0;
|
||||
PayPoints = 0;
|
||||
PointsExchangeMoney = decimal.Zero;
|
||||
IsPushMessage = false;
|
||||
CostAmount = decimal.Zero;
|
||||
OrderAmount = decimal.Zero;
|
||||
RealOrderAmount = decimal.Zero;
|
||||
AccountsDueAmount = decimal.Zero;
|
||||
SettleType = 0;
|
||||
IsPushMessage = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订单号
|
||||
/// </summary>
|
||||
public string OrderSn { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 客户编号
|
||||
/// </summary>
|
||||
public string CustomerNo { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 收货人姓名
|
||||
/// </summary>
|
||||
public string CustomerName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 成本总金额
|
||||
/// </summary>
|
||||
public decimal CostAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 订单总金额
|
||||
/// </summary>
|
||||
public decimal OrderAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实收金额(整单优惠后)
|
||||
/// </summary>
|
||||
public decimal RealOrderAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销货日期
|
||||
/// </summary>
|
||||
public DateTime SaleDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 下单时间
|
||||
/// </summary>
|
||||
public DateTime AddTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 媒体资源投放ID
|
||||
/// </summary>
|
||||
public string IndustryCode { get; set; }
|
||||
|
||||
public string IndustryName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 班组
|
||||
/// </summary>
|
||||
public int Team { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销售员编号
|
||||
/// </summary>
|
||||
public string SellerNo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销售员姓名
|
||||
/// </summary>
|
||||
public string SellerName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 操作人ID
|
||||
/// </summary>
|
||||
public virtual string HandlerCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 操作者
|
||||
/// </summary>
|
||||
public string Handler { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 发货仓库代号
|
||||
/// </summary>
|
||||
public string StoreCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 发货仓库名称
|
||||
/// </summary>
|
||||
public string StoreName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销货店铺渠道代号
|
||||
/// </summary>
|
||||
public string ShopChannelCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 销货店铺渠道名称
|
||||
/// </summary>
|
||||
public string ShopChannelName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 订单产品数
|
||||
/// </summary>
|
||||
public int GoodsNum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 礼品数量
|
||||
/// </summary>
|
||||
public int GiftNum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 对应预订单号
|
||||
/// </summary>
|
||||
public string CustomerOrderSn { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 订单赠送积分
|
||||
/// </summary>
|
||||
public int Points { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 应收款金额
|
||||
/// </summary>
|
||||
public decimal AccountsDueAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 来自预约单号
|
||||
/// </summary>
|
||||
public string ReserationOrderSn { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 订单状态 0为未审核 1为已审核
|
||||
/// </summary>
|
||||
public int OrderStatus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 审核人
|
||||
/// </summary>
|
||||
public string CheckMan { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 审核时间
|
||||
/// </summary>
|
||||
public DateTime? CheckTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结算类型 0为非金工石(零售) 1为金工石
|
||||
/// </summary>
|
||||
public int SettleType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 使用积分
|
||||
/// </summary>
|
||||
public int PayPoints { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 积分抵现金额
|
||||
/// </summary>
|
||||
public decimal PointsExchangeMoney { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已推送微信消息
|
||||
/// </summary>
|
||||
public bool IsPushMessage { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class SaleOrderBaseInfo
|
||||
{
|
||||
public int GoodsNum { get; set; }
|
||||
|
||||
public int GiftNum { get; set; }
|
||||
|
||||
public decimal OrderAmount { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class UnitTest3
|
||||
{
|
||||
public DateTime? Date { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class UnitSelectTest2
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)]
|
||||
public decimal? DcNull { get; set; }
|
||||
public decimal Dc { get; set; }
|
||||
public int? IntNull { get; set; }
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public decimal Int { get; set; }
|
||||
}
|
||||
|
||||
public class UnitSelectTest
|
||||
{
|
||||
public decimal? DcNull { get; set; }
|
||||
public decimal Dc { get; set; }
|
||||
public int? IntNull { get; set; }
|
||||
public decimal Int { get; set; }
|
||||
}
|
||||
|
||||
public class UnitGuidTable
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
43
Src/Asp.NetCore2/HGTest/UnitTest/UQueryableAsync.cs
Normal file
43
Src/Asp.NetCore2/HGTest/UnitTest/UQueryableAsync.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
namespace OrmTest
|
||||
{
|
||||
public partial class NewUnitTest
|
||||
{
|
||||
public static void QueryableAsync()
|
||||
{
|
||||
q1();
|
||||
q2();
|
||||
q3();
|
||||
}
|
||||
|
||||
private static void q1()
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var count = Db.Queryable<Order>().Count();
|
||||
Task t = Db.Queryable<Order>().ToPageListAsync(1, 2, total);
|
||||
t.Wait();
|
||||
UValidate.Check(count, total.Value, "QueryableAsync");
|
||||
}
|
||||
private static void q2()
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var count = Db.Queryable<Order>().Count();
|
||||
Task t = Db.Queryable<Order>().ToDataTablePageAsync(1, 2, total);
|
||||
t.Wait();
|
||||
UValidate.Check(count, total.Value, "QueryableAsync");
|
||||
}
|
||||
private static void q3()
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var count = Db.Queryable<Order>().Count();
|
||||
Task t = Db.Queryable<Order>().ToJsonPageAsync(1, 2, total);
|
||||
t.Wait();
|
||||
UValidate.Check(count, total.Value, "QueryableAsync");
|
||||
}
|
||||
}
|
||||
}
|
378
Src/Asp.NetCore2/HGTest/UnitTest/UThread.cs
Normal file
378
Src/Asp.NetCore2/HGTest/UnitTest/UThread.cs
Normal file
@ -0,0 +1,378 @@
|
||||
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 SqlSugarClient simpleDb => new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static SqlSugarScope ssDb => new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
//IsShardSameThread = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static SqlSugarScope singleDb = new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static SqlSugarScope singleAndSsDb = new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.HG,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
//IsShardSameThread = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
public static void Thread()
|
||||
{
|
||||
Simple();
|
||||
IsShardSameThread();
|
||||
Single();
|
||||
SingleAndIsShardSameThread();
|
||||
SimpleAsync();
|
||||
IsShardSameThreadAsync();
|
||||
SingleAsync();
|
||||
SingleAndIsShardSameThreadAsync();
|
||||
|
||||
}
|
||||
|
||||
private static void Simple()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThread()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void Single()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThread()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void SimpleAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait(); ;
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThreadAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleAndSsDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
singleDb.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThreadAsync()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test2", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Db.Insertable(new Order() { Name = "test3", CreateTime = DateTime.Now }).ExecuteCommandAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
}
|
||||
}
|
316
Src/Asp.NetCore2/HGTest/UnitTest/UThread2.cs
Normal file
316
Src/Asp.NetCore2/HGTest/UnitTest/UThread2.cs
Normal file
@ -0,0 +1,316 @@
|
||||
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 Thread2()
|
||||
{
|
||||
Simple2();
|
||||
IsShardSameThread2();
|
||||
Single2();
|
||||
SingleAndIsShardSameThread2();
|
||||
SimpleAsync2();
|
||||
IsShardSameThreadAsync2();
|
||||
SingleAsync2();
|
||||
SingleAndIsShardSameThreadAsync2();
|
||||
|
||||
}
|
||||
|
||||
private static void Simple2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThread2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void Single2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThread2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToList();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void SimpleAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait(); ;
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAndIsShardSameThreadAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void SingleAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
|
||||
private static void IsShardSameThreadAsync2()
|
||||
{
|
||||
var t1 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(1);
|
||||
}
|
||||
|
||||
});
|
||||
var t2 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
});
|
||||
var t3 = new Task(() =>
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
simpleDb.Queryable<Order>().Take(10).ToListAsync().Wait();
|
||||
System.Threading.Thread.Sleep(6);
|
||||
}
|
||||
|
||||
});
|
||||
t1.Start();
|
||||
t2.Start();
|
||||
t3.Start();
|
||||
|
||||
Task.WaitAll(t1, t2, t3);
|
||||
}
|
||||
}
|
||||
}
|
117
Src/Asp.NetCore2/HGTest/UnitTest/UThread3.cs
Normal file
117
Src/Asp.NetCore2/HGTest/UnitTest/UThread3.cs
Normal file
@ -0,0 +1,117 @@
|
||||
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 Thread3()
|
||||
{
|
||||
Console.WriteLine("Thread3");
|
||||
SimpleAsync3().Wait();
|
||||
IsShardSameThreadAsync3().Wait();
|
||||
SingleAsync3().Wait();
|
||||
SingleAndIsShardSameThreadAsync3().Wait();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static async Task SimpleAsync3()
|
||||
{
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await simpleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await simpleDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await simpleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SingleAndIsShardSameThreadAsync3()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleAndSsDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleAndSsDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await singleAndSsDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SingleAsync3()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await singleDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await singleDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task IsShardSameThreadAsync3()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await ssDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
await ssDb.Insertable(new Order() { Name = "a", CustomId = 1 }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
List<Order> orders = new List<Order>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
orders = await ssDb.Queryable<Order>().Take(10).ToListAsync();
|
||||
}
|
||||
if (orders.Count > 0)
|
||||
{
|
||||
Console.WriteLine("async is ok");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
Src/Asp.NetCore2/HGTest/UnitTest/UValidate.cs
Normal file
19
Src/Asp.NetCore2/HGTest/UnitTest/UValidate.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class UValidate
|
||||
{
|
||||
public static void Check(object a, object b, object name)
|
||||
{
|
||||
if (a?.ToString()?.Trim() != b?.ToString()?.Trim())
|
||||
{
|
||||
throw new Exception(name + " error");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
120
Src/Asp.NetCore2/HGTest/UnitTest/Unit001.cs
Normal file
120
Src/Asp.NetCore2/HGTest/UnitTest/Unit001.cs
Normal file
@ -0,0 +1,120 @@
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using SqlSugar;
|
||||
namespace OrmTest {
|
||||
public static partial class ObjectExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 排除SqlSugar忽略的列
|
||||
/// </summary>
|
||||
/// <param name="pi"></param>
|
||||
/// <returns></returns>
|
||||
private static bool IsIgnoreColumn(PropertyInfo pi)
|
||||
{
|
||||
var sc = pi.GetCustomAttributes<SugarColumn>(false).FirstOrDefault(u => u.IsIgnore == true);
|
||||
return sc != null;
|
||||
}
|
||||
/// <summary>
|
||||
/// List转DataTable
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="list"></param>
|
||||
/// <returns></returns>
|
||||
public static DataTable ToDataTable<T>(this List<T> list)
|
||||
{
|
||||
DataTable result = new DataTable();
|
||||
if (list.Count > 0)
|
||||
{
|
||||
// result.TableName = list[0].GetType().Name; // 表名赋值
|
||||
PropertyInfo[] propertys = list[0].GetType().GetProperties();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
Type colType = pi.PropertyType;
|
||||
if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
colType = colType.GetGenericArguments()[0];
|
||||
}
|
||||
if (IsIgnoreColumn(pi))
|
||||
continue;
|
||||
result.Columns.Add(pi.Name, colType);
|
||||
}
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
ArrayList tempList = new ArrayList();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
if (IsIgnoreColumn(pi))
|
||||
continue;
|
||||
object obj = pi.GetValue(list[i], null);
|
||||
tempList.Add(obj);
|
||||
}
|
||||
object[] array = tempList.ToArray();
|
||||
result.LoadDataRow(array, true);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Unit001
|
||||
{
|
||||
|
||||
public static void Init( )
|
||||
{
|
||||
var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
|
||||
{
|
||||
ConnectionString =Config.ConnectionString2,
|
||||
DbType = SqlSugar.DbType.HG,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
||||
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
|
||||
db.Aop.OnLogExecuting = (sql,p) =>
|
||||
{
|
||||
|
||||
Console.WriteLine(sql);
|
||||
|
||||
};
|
||||
|
||||
//建表
|
||||
if (!db.DbMaintenance.IsAnyTable("Test0011", false))
|
||||
{
|
||||
db.CodeFirst.InitTables<Test0011>();
|
||||
}
|
||||
List<Test0011> list = new List<Test0011>();
|
||||
list.Add(new Test0011() { Id = 1 });
|
||||
System.Type entityType = typeof(Test0011);
|
||||
var seedDataTable = db.Utilities.ListToDataTable(list);
|
||||
seedDataTable.TableName = db.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
|
||||
var storage = db.Storageable(seedDataTable).WhereColumns("id").ToStorage();
|
||||
var result = storage.AsInsertable.ExecuteCommand();
|
||||
|
||||
|
||||
|
||||
Console.WriteLine(result);
|
||||
Console.WriteLine("用例跑完");
|
||||
|
||||
}
|
||||
//建类
|
||||
public class Test0011
|
||||
{
|
||||
[SugarColumn(ColumnDescription = "Id", IsPrimaryKey = true, IsIdentity = false)]
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "更新时间", IsOnlyIgnoreInsert = true,IsNullable =true)]
|
||||
public DateTime? UpdateTime { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
350
Src/Asp.NetCore2/HGTest/UnitTest/UnitSubToList.cs
Normal file
350
Src/Asp.NetCore2/HGTest/UnitTest/UnitSubToList.cs
Normal file
@ -0,0 +1,350 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace OrmTest
|
||||
{
|
||||
public class UnitSubToList
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = NewUnitTest.Db;
|
||||
// db.Aop.OnLogExecuting = null;
|
||||
|
||||
db.CodeFirst.InitTables<Order, OrderItem, Custom>();
|
||||
db.DbMaintenance.DropTable<Order, OrderItem, Custom>();
|
||||
db.CodeFirst.InitTables<Order, OrderItem, Custom>();
|
||||
db.Insertable(new Order() { Id = 1, Name = "订单01", CustomId = 1, Price = 111, CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
db.Insertable(new Order() { Id = 1, Name = "订单011", CustomId = 1, Price = 1111, CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
db.Insertable(new Order() { Id = 1, Name = "订单033", CustomId = 3, Price = 31, CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
db.Insertable(new Order() { Id = 1, Name = "订单03", CustomId = 3, Price = 331, CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
db.Insertable(new Order() { Id = 1, Name = "订单04", CustomId = 4, Price = 411, CreateTime = DateTime.Now }).ExecuteCommand();
|
||||
|
||||
db.Insertable(new Custom() { Id = 1, Name = "客户1", }).ExecuteCommand();
|
||||
db.Insertable(new Custom() { Id = 3, Name = "客户3", }).ExecuteCommand();
|
||||
db.Insertable(new Custom() { Id = 4, Name = "客户4", }).ExecuteCommand();
|
||||
|
||||
db.Insertable(new OrderItem() { ItemId = 1, OrderId =1, Price=1 }).ExecuteCommand();
|
||||
db.Insertable(new OrderItem() { ItemId = 3, OrderId =3, Price=3}).ExecuteCommand();
|
||||
db.Insertable(new OrderItem() { ItemId = 4, OrderId=4 , Price=4}).ExecuteCommand();
|
||||
|
||||
TestGetAll(db);
|
||||
TestWhere(db);
|
||||
TestJoin(db);
|
||||
TestJoin2(db);
|
||||
TestJoin3(db);
|
||||
TestJoin4(db);
|
||||
}
|
||||
private static void TestJoin4(SqlSugarClient db)
|
||||
{
|
||||
var test1 = db.Queryable<Order>()
|
||||
.LeftJoin<Custom>((o, c) => c.Id == o.CustomId)
|
||||
.Select((o, c) => new myDTO5
|
||||
{
|
||||
OrderName = o.Name,
|
||||
disCount = SqlFunc.Subqueryable<Custom>().Where(d => d.Name == c.Name).ToList()
|
||||
},
|
||||
true)
|
||||
.ToList();
|
||||
if (test1.Any(z => z.disCount.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
var test2= db.Queryable<Order>()
|
||||
.Select(o => new myDTO5
|
||||
{
|
||||
disCount = SqlFunc.Subqueryable<Custom>().Where(d => d.Id == o.CustomId).ToList()
|
||||
},
|
||||
true)
|
||||
.ToList();
|
||||
if (test2.Any(z => z.disCount.Any(y => y.Id != z.CustomId))||test2.Any(z=>z.Id==0))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
}
|
||||
private static void TestJoin3(SqlSugarClient db)
|
||||
{
|
||||
var test1 = db.Queryable<Order>()
|
||||
.LeftJoin<Custom>((o, c) => c.Id == o.CustomId)
|
||||
.Select((o, c) => new
|
||||
{
|
||||
CustomId = o.CustomId,
|
||||
OrderId = o.Id,
|
||||
OrderName = o.Name,
|
||||
disCount = SqlFunc.Subqueryable<Custom>().Where(d => d.Name == c.Name).ToList()
|
||||
})
|
||||
.ToList();
|
||||
if (test1.Any(z => z.disCount.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
var test2 = db.Queryable<Custom>()
|
||||
.LeftJoin<Order>((o, c) => c.CustomId == o.Id)
|
||||
.Select((o, c) => new
|
||||
{
|
||||
Id=o.Id,
|
||||
cusName = o.Name,
|
||||
Orders = SqlFunc.Subqueryable<Order>().Where(d => d.CustomId == o.Id).ToList()
|
||||
})
|
||||
.ToList();
|
||||
if (test2.Any(z => z.Orders.Any(y => y.CustomId != z.Id))|| test2.First().Orders.Count()==0)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
}
|
||||
private static void TestJoin2(SqlSugarClient db)
|
||||
{
|
||||
var test1 = db.Queryable<Order>()
|
||||
.LeftJoin<Custom>((o, c) => c.Id == o.CustomId)
|
||||
.LeftJoin<OrderItem>((o, c, i) => i.OrderId == o.Id)
|
||||
.Select((o, c, i) => new
|
||||
{
|
||||
itemId = i.ItemId,
|
||||
CustomId = o.CustomId,
|
||||
OrderId = o.Id,
|
||||
OrderName = o.Name,
|
||||
disCount = SqlFunc.Subqueryable<OrderItem>().Where(d => d.ItemId == i.ItemId).ToList()
|
||||
})
|
||||
.ToList();
|
||||
if (test1.Any(z => z.disCount.Any(y => y.ItemId != z.itemId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
var test2 = db.Queryable<Order>()
|
||||
.LeftJoin<Custom>((o, c) => c.Id == o.CustomId)
|
||||
.LeftJoin<OrderItem>((o, c, i) => i.OrderId == o.Id)
|
||||
.Select((o, c, i) => new
|
||||
{
|
||||
itemId = i.ItemId,
|
||||
CustomId = o.CustomId,
|
||||
CusName = SqlFunc.Subqueryable<Custom>().Where(s => s.Id == o.CustomId).Select(s => s.Name),
|
||||
OrderId = o.Id,
|
||||
cusList = SqlFunc.Subqueryable<Custom>().Where(d => d.Id == o.CustomId).ToList(),
|
||||
OrderName = o.Name,
|
||||
disCount = SqlFunc.Subqueryable<OrderItem>().Where(d => d.ItemId == i.ItemId).ToList()
|
||||
})
|
||||
.ToList();
|
||||
if (test2.Any(z => z.disCount.Any(y => y.ItemId != z.itemId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
if (test2.Any(z => z.disCount.Any(y => y.ItemId != z.itemId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
if (test2.Any(z => z.cusList.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
if (test2.Any(z => z.cusList.Any(y => y.Name != z.CusName)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
}
|
||||
private static void TestJoin(SqlSugarClient db)
|
||||
{
|
||||
var test1 = db.Queryable<Order>()
|
||||
.LeftJoin<Custom>((o,c)=>c.Id==o.CustomId)
|
||||
.LeftJoin<OrderItem>((o, c, i) => i.OrderId==o.Id)
|
||||
.Select((o,c,i) => new myDTO4
|
||||
{
|
||||
itemId=i.ItemId,
|
||||
CustomId = o.CustomId,
|
||||
OrderId = o.Id,
|
||||
OrderName = o.Name,
|
||||
disCount = SqlFunc.Subqueryable<OrderItem>().Where(d => d.ItemId == i.ItemId).ToList()
|
||||
})
|
||||
.ToList();
|
||||
if (test1.Any(z => z.disCount.Any(y => y.ItemId != z.itemId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
var test2 = db.Queryable<Order>()
|
||||
.LeftJoin<Custom>((o, c) => c.Id == o.CustomId)
|
||||
.LeftJoin<OrderItem>((o, c, i) => i.OrderId == o.Id)
|
||||
.Select((o, c, i) => new myDTO4
|
||||
{
|
||||
itemId = i.ItemId,
|
||||
CustomId = o.CustomId,
|
||||
CusName=SqlFunc.Subqueryable<Custom>().Where(s=>s.Id==o.CustomId).Select(s=>s.Name),
|
||||
OrderId = o.Id,
|
||||
cusList= SqlFunc.Subqueryable<Custom>().Where(d => d.Id == o.CustomId).ToList(),
|
||||
OrderName = o.Name,
|
||||
disCount = SqlFunc.Subqueryable<OrderItem>().Where(d => d.ItemId == i.ItemId).ToList()
|
||||
})
|
||||
.ToList();
|
||||
if (test2.Any(z => z.disCount.Any(y => y.ItemId != z.itemId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
if (test2.Any(z => z.disCount.Any(y => y.ItemId != z.itemId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
if (test2.Any(z => z.cusList.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
if (test2.Any(z => z.cusList.Any(y => y.Name != z.CusName)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
}
|
||||
private static void TestWhere(SqlSugarClient db)
|
||||
{
|
||||
var test1 = db.Queryable<Order>().Select(it => new myDTO3
|
||||
{
|
||||
CustomId = it.CustomId,
|
||||
OrderId=it.Id,
|
||||
OrderName=it.Name,
|
||||
disCount = SqlFunc.Subqueryable<Custom>().Where(c=>c.Id==it.CustomId).ToList()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
if (test1.Any(z => z.disCount.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
var test2 = db.Queryable<Order>().Select(it => new
|
||||
{
|
||||
CustomId = it.CustomId,
|
||||
OrderId = it.Id,
|
||||
OrderName = it.Name,
|
||||
disCount = SqlFunc.Subqueryable<Custom>().Where(c => c.Id == it.CustomId).ToList()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
|
||||
if (test2.Any(z => z.disCount.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
|
||||
var test3 = db.Queryable<Order>().Select(it => new myDTO3
|
||||
{
|
||||
CustomId = it.CustomId,
|
||||
OrderId = it.Id,
|
||||
OrderName = it.Name,
|
||||
disCount = SqlFunc.Subqueryable<Custom>().Where(c => c.Id == it.CustomId).ToList()
|
||||
})
|
||||
.ToListAsync().GetAwaiter().GetResult();
|
||||
|
||||
if (test3.Any(z => z.disCount.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
var test4 = db.Queryable<Order>().Select(it => new
|
||||
{
|
||||
CustomId = it.CustomId,
|
||||
OrderId = it.Id,
|
||||
OrderName = it.Name,
|
||||
disCount = SqlFunc.Subqueryable<Custom>().Where(c => c.Id == it.CustomId).ToList()
|
||||
})
|
||||
.ToListAsync().GetAwaiter().GetResult();
|
||||
|
||||
if (test4.Any(z => z.disCount.Any(y => y.Id != z.CustomId)))
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
}
|
||||
private static void TestGetAll(SqlSugarClient db)
|
||||
{
|
||||
var test1 = db.Queryable<Order>().Select(it => new myDTO
|
||||
{
|
||||
Id = it.Id,
|
||||
disCount = SqlFunc.Subqueryable<Order>().ToList()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
if (test1.First().disCount.Count != test1.Count)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
var test2 = db.Queryable<Order>().Select(it => new
|
||||
{
|
||||
Id = it.Id,
|
||||
disCount = SqlFunc.Subqueryable<Order>().ToList()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
if (test2.First().disCount.Count != test2.Count)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
var test3 = db.Queryable<Order>().Select(it => new myDTO
|
||||
{
|
||||
Id = it.Id,
|
||||
disCount = SqlFunc.Subqueryable<Order>().ToList()
|
||||
})
|
||||
.ToListAsync().GetAwaiter().GetResult();
|
||||
|
||||
if (test3.First().disCount.Count != test3.Count)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
var test4 = db.Queryable<Order>().Select(it => new
|
||||
{
|
||||
Id = it.Id,
|
||||
disCount = SqlFunc.Subqueryable<Order>().ToList()
|
||||
})
|
||||
.ToListAsync().GetAwaiter().GetResult();
|
||||
|
||||
if (test4.First().disCount.Count != test4.Count)
|
||||
{
|
||||
throw new Exception("unit error");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class myDTO
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public List<Order> disCount { get; set; }
|
||||
}
|
||||
internal class myDTO2
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public List<OrderItem> disCount { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
internal class myDTO5
|
||||
{
|
||||
public int CustomId { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public string OrderName { get; set; }
|
||||
public List<Custom> disCount { get; set; }
|
||||
public int Id { get; internal set; }
|
||||
}
|
||||
|
||||
internal class myDTO4
|
||||
{
|
||||
public int CustomId { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public string OrderName { get; set; }
|
||||
public List<OrderItem> disCount { get; set; }
|
||||
public int itemId { get; internal set; }
|
||||
public List<Custom> cusList { get; internal set; }
|
||||
public string CusName { get; internal set; }
|
||||
}
|
||||
|
||||
internal class myDTO3
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public List<Custom> disCount { get; set; }
|
||||
public int OrderId { get; internal set; }
|
||||
public string OrderName { get; internal set; }
|
||||
public int CustomId { get; internal set; }
|
||||
}
|
||||
}
|
83
Src/Asp.NetCore2/HGTest/UnitTest/UnitTestReturnPkList.cs
Normal file
83
Src/Asp.NetCore2/HGTest/UnitTest/UnitTestReturnPkList.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class UnitTestReturnPkList
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
TestIdentity();
|
||||
TestLong();
|
||||
TestGuid();
|
||||
}
|
||||
|
||||
private static void TestGuid()
|
||||
{
|
||||
var db = NewUnitTest.Db;
|
||||
db.CodeFirst.InitTables<UnitGuid123>();
|
||||
var ids = db.Insertable(new UnitGuid123() { Name = "a" }).ExecuteReturnPkList<Guid>();
|
||||
if (ids.Count() != 1) { throw new Exception("unit error"); }
|
||||
if (db.Queryable<UnitGuid123>().In(ids).Count() !=1) { throw new Exception("unit error"); }
|
||||
var ids2 = db.Insertable(new List<UnitGuid123>() { new UnitGuid123() { Name = "c" }, new UnitGuid123() { Name = "c" } }).ExecuteReturnPkList<Guid>();
|
||||
if (ids2.Count() != 2) { throw new Exception("unit error"); }
|
||||
if (db.Queryable<UnitGuid123>().In(ids2).Count() != 2) { throw new Exception("unit error"); }
|
||||
}
|
||||
|
||||
private static void TestLong()
|
||||
{
|
||||
var db = NewUnitTest.Db;
|
||||
db.CodeFirst.InitTables<UnitLong1231>();
|
||||
var ids = db.Insertable(new UnitLong1231() { Name = "a" }).ExecuteReturnPkList<long>();
|
||||
if (ids.Count(z => z > 0) != 1) { throw new Exception("unit error"); }
|
||||
var ids2 = db.Insertable(new List<UnitLong1231>() { new UnitLong1231() { Name = "c" }, new UnitLong1231() { Name = "c" } }).ExecuteReturnPkList<long>();
|
||||
if (ids2.Count(z => z > 0) != 2) { throw new Exception("unit error"); }
|
||||
if(db.Queryable<UnitLong1231>().In(ids2).Count()!=2) { throw new Exception("unit error"); }
|
||||
}
|
||||
|
||||
private static void TestIdentity()
|
||||
{
|
||||
var db = NewUnitTest.Db;
|
||||
db.CodeFirst.InitTables<UnitIdentity01>();
|
||||
var ids = db.Insertable(new UnitIdentity01() { Name = "a" }).ExecuteReturnPkList<int>();
|
||||
if (ids.Count(z => z > 0) != 1) { throw new Exception("unit error"); }
|
||||
var ids2 = db.Insertable(new List<UnitIdentity01>() { new UnitIdentity01() { Name = "c" }, new UnitIdentity01() { Name = "c" } }).ExecuteReturnPkList<int>();
|
||||
if (ids2.Count(z => z > 0) != 2) { throw new Exception("unit error"); }
|
||||
if (db.Queryable<UnitIdentity01>().In(ids2).Count() != 2) { throw new Exception("unit error"); }
|
||||
var list = new List<UnitIdentity01>();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
list.Add(new UnitIdentity01() { Name = "a" });
|
||||
}
|
||||
var ids3 = db.Insertable(list).ExecuteReturnPkList<int>();
|
||||
if (db.Queryable<UnitIdentity01>().In(ids3).Count() != 1000) { throw new Exception("unit error"); }
|
||||
db.DbMaintenance.TruncateTable<UnitIdentity01>();
|
||||
}
|
||||
|
||||
public class UnitGuid123
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public Guid id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
public class UnitLong1231
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public long id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public class UnitIdentity01
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsIdentity =true,IsPrimaryKey =true)]
|
||||
public int id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
211
Src/Asp.NetCore2/HGTest/UnitTest/Updateable.cs
Normal file
211
Src/Asp.NetCore2/HGTest/UnitTest/Updateable.cs
Normal file
@ -0,0 +1,211 @@
|
||||
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<UnitUser>();
|
||||
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<UnitBoolTest>().SetColumns(it => new UnitBoolTest() { BoolValue = !it.BoolValue }).Where(it=>it.Id==1).ExecuteCommand();
|
||||
//Db.Updateable<UnitBoolTest>().SetColumns(it => it.BoolValue == !it.BoolValue ).Where(it=>it.Id==1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => new UnitBoolTest() { BoolValue = x.BoolValue }).Where(it => it.Id == 1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => it.BoolValue == x.BoolValue).Where(it => it.Id == 1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>().SetColumns(it => new UnitBoolTest() { BoolValue = !x.BoolValue }).Where(it => it.Id == 1).ExecuteCommand();
|
||||
//Db.Updateable<UnitBoolTest>().SetColumns(it => it.BoolValue == !x.BoolValue).Where(it => it.Id == 1).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>(x).ReSetValue(it => it.BoolValue = it.BoolValue).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>(x).ReSetValue(it => it.BoolValue = true).ExecuteCommand();
|
||||
//Db.Updateable<UnitBoolTest>(x).ReSetValue(it => it.BoolValue == !it.BoolValue).ExecuteCommand();
|
||||
Db.Updateable<UnitBoolTest>(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<UnitDiary>().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<UnitDiary>().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");
|
||||
|
||||
}
|
||||
}
|
||||
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; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 日记表
|
||||
/// </summary>
|
||||
[SugarTable("Diary")]
|
||||
public class UnitDiary
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int ID { get; set; }
|
||||
/// <summary>
|
||||
/// 用户ID
|
||||
/// </summary>
|
||||
public int? UserID { get; set; }
|
||||
/// <summary>
|
||||
/// 日记类型ID
|
||||
/// </summary>
|
||||
public int? TypeID { get; set; }
|
||||
/// <summary>
|
||||
/// 日记类型名称
|
||||
/// </summary>
|
||||
public string TypeName { get; set; }
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
public string Title { get; set; }
|
||||
/// <summary>
|
||||
/// 内容
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
/// <summary>
|
||||
/// 时间
|
||||
/// </summary>
|
||||
public DateTime? Time { get; set; }
|
||||
/// <summary>
|
||||
/// 是否提醒
|
||||
/// </summary>
|
||||
public bool? IsRemind { get; set; }
|
||||
/// <summary>
|
||||
/// 封面图
|
||||
/// </summary>
|
||||
public string Cover { get; set; }
|
||||
/// <summary>
|
||||
/// 是否为系统日记 1:系统日记 0:用户日记
|
||||
/// </summary>
|
||||
public bool? IsSystem { get; set; }
|
||||
/// <summary>
|
||||
/// 权重(排序)
|
||||
/// </summary>
|
||||
public int? Sequence { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string IP { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public DateTime? CreateTime { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public DateTime? UpdateTime { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 普通用户表
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class UnitUser
|
||||
{
|
||||
private System.Int64? _USER_ID;
|
||||
/// <summary>
|
||||
/// GUID主键
|
||||
/// </summary>
|
||||
[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;
|
||||
/// <summary>
|
||||
/// 用户账号,不可重名,即使是假删除了,亦不可重复
|
||||
/// </summary>
|
||||
public System.String USER_ACCOUNT { get { return this._USER_ACCOUNT; } set { this._USER_ACCOUNT = value; } }
|
||||
|
||||
private System.String _USER_PWD;
|
||||
/// <summary>
|
||||
/// 用户密码
|
||||
/// </summary>
|
||||
public System.String USER_PWD
|
||||
{
|
||||
get { return this._USER_PWD; }
|
||||
set { this._USER_PWD = value; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 不允许用户密码序列化,可以反序列化
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool ShouldSerializeUSER_PWD()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
private System.String _USER_NAME;
|
||||
/// <summary>
|
||||
/// 用户姓名
|
||||
/// </summary>
|
||||
|
||||
public System.String USER_NAME { get { return this._USER_NAME; } set { this._USER_NAME = value; } }
|
||||
|
||||
private System.Int32 _USER_STATUS;
|
||||
/// <summary>
|
||||
/// 用户状体:10正常;20锁定;99已删除
|
||||
/// </summary>
|
||||
public System.Int32 USER_STATUS { get { return this._USER_STATUS; } set { this._USER_STATUS = value; } }
|
||||
|
||||
private System.DateTime _PWD_LASTCHTIME;
|
||||
/// <summary>
|
||||
/// 最后一次密码更新时间
|
||||
/// </summary>
|
||||
public System.DateTime PWD_LASTCHTIME { get { return this._PWD_LASTCHTIME; } set { this._PWD_LASTCHTIME = value; } }
|
||||
|
||||
private System.Int32? _PWD_ERRORCOUNT;
|
||||
/// <summary>
|
||||
/// 密码错误次数,达到定义的次数就锁定
|
||||
/// </summary>
|
||||
public System.Int32? PWD_ERRORCOUNT { get { return this._PWD_ERRORCOUNT; } set { this._PWD_ERRORCOUNT = value ?? 0; } }
|
||||
|
||||
private System.DateTime? _PWD_LASTERRTIME = null;
|
||||
/// <summary>
|
||||
/// 密码最后一次错误时间,满足定义的时间差之后,可自动解锁,如20分钟后自动解锁,亦作为累次错误次数的时间差比对基础
|
||||
/// 允许为空
|
||||
/// </summary>
|
||||
public System.DateTime? PWD_LASTERRTIME { get { return this._PWD_LASTERRTIME; } set { this._PWD_LASTERRTIME = value; } }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user