mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Add Demo
This commit is contained in:
parent
92af9e4e66
commit
69573308b1
@ -63,7 +63,7 @@ namespace OrmTest
|
||||
}
|
||||
|
||||
var list = new List<Users>();
|
||||
for (var i = 0; i < 100000; i++)
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
|
||||
list.Add(new Users
|
||||
|
@ -19,22 +19,34 @@ namespace OrmTest
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
db.CodeFirst.InitTables(typeof(CodeFirstTable1));//Create CodeFirstTable1
|
||||
db.Insertable(new CodeFirstTable1() { Name = "a", Text="a" }).ExecuteCommand();
|
||||
db.Insertable(new CodeFirstTable1() { Name = "a", Text = "a" }).ExecuteCommand();
|
||||
var list = db.Queryable<CodeFirstTable1>().ToList();
|
||||
db.CodeFirst.InitTables<IndexClass>();
|
||||
db.CodeFirst.InitTables<SplitTableEntity>();
|
||||
Console.WriteLine("#### CodeFirst end ####");
|
||||
}
|
||||
}
|
||||
|
||||
[SugarIndex(null,nameof(IndexClass.Name),OrderByType.Asc)]
|
||||
public class IndexClass
|
||||
[SugarIndex(null, nameof(IndexClass.Name), OrderByType.Asc)]
|
||||
public class IndexClass
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[SugarColumn(ColumnDataType ="symbol")]
|
||||
[SugarColumn(ColumnDataType = "symbol")]
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class SplitTableEntity
|
||||
{
|
||||
public string Id { get; set; }
|
||||
[TimeDbSplitField(DateType.Day)]
|
||||
public DateTime Ts { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class CodeFirstTable1
|
||||
{
|
||||
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
|
||||
|
@ -1,96 +0,0 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoN_SplitTable
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### DemoN_SplitTable Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.QuestDB,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
db.Aop.OnLogExecuted = (s, p) =>
|
||||
{
|
||||
Console.WriteLine(s);
|
||||
};
|
||||
|
||||
//初始化分表
|
||||
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//根据最近3个表进行查询
|
||||
var list=db.Queryable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid())
|
||||
.SplitTable(tabs => tabs.Take(3))
|
||||
.Where(it=>it.Time==DateTime.Now).ToOffsetPage(1,2);
|
||||
|
||||
|
||||
var first = db.Queryable<OrderSpliteTest>()
|
||||
.SplitTable(DateTime.MaxValue, DateTime.Now)
|
||||
.First();//no table
|
||||
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//根据时间选出的表进行查询
|
||||
var list2 = db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Where(it=> it.Date>=DateTime.Now.AddYears(-2))).ToList();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//删除数据只在最近3张表执行操作
|
||||
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
var tableName = db.SplitHelper<OrderSpliteTest>().GetTableName(DateTime.Now.AddDays(-1));
|
||||
var tableName2 = db.SplitHelper(new OrderSpliteTest() { Time=DateTime.Now}).GetTableNames();
|
||||
var tableName3 = db.SplitHelper(new List<OrderSpliteTest> {
|
||||
new OrderSpliteTest() { Time = DateTime.Now },
|
||||
new OrderSpliteTest() { Time = DateTime.Now },
|
||||
new OrderSpliteTest() { Time = DateTime.Now.AddMonths(-10) }
|
||||
}).GetTableNames();
|
||||
var x2 = db.Updateable<OrderSpliteTest>()
|
||||
.SetColumns(it=>it.Name=="a")
|
||||
.Where(it => it.Pk == Guid.NewGuid())
|
||||
.SplitTable(tabs => tabs.InTableNames(tableName2))
|
||||
.ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//按日分表
|
||||
var x3 = db.Insertable(new OrderSpliteTest() { Name="A" }).SplitTable().ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
////强制分表类型
|
||||
var x4 = db.Insertable(new OrderSpliteTest() { Name = "A" ,Time=DateTime.Now.AddDays(-1) }).SplitTable().ExecuteCommand();
|
||||
|
||||
|
||||
var tableName21 = db.SplitHelper<OrderSpliteTest>().GetTableName(DateTime.Now.AddDays(-111));
|
||||
var listNull = db.Queryable<OrderSpliteTest>().SplitTable(ta => ta.InTableNames(tableName21)).ToList();
|
||||
Console.WriteLine("#### CodeFirst end ####");
|
||||
}
|
||||
|
||||
[SplitTable(SplitType.Day)]
|
||||
[SqlSugar.SugarTable("Taxxx0101_{year}{month}{day}")]
|
||||
public class OrderSpliteTest
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey =true)]
|
||||
public Guid Pk{ get; set; }
|
||||
public string Name { get; set; }
|
||||
[SugarColumn(IsNullable =true)]
|
||||
[SplitField]
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user