SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.2 KiB

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("#### CodeFirst Start ####");
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
3 years ago
db.Aop.OnLogExecuted = (s, p) =>
{
Console.WriteLine(s);
3 years ago
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
3 years ago
};
3 years ago
3 years ago
//初始化分表
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
3 years ago
3 years ago
Console.WriteLine();
3 years ago
//根据最近3个表进行查询
3 years ago
var list=db.Queryable<OrderSpliteTest>()
.SplitTable(DateTime.Now.Date.AddYears(-1),DateTime.Now)
.ToList();
3 years ago
3 years ago
Console.WriteLine();
3 years ago
//根据时间选出的表进行查询
3 years ago
var list2 = db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Where(it=> it.Date>=DateTime.Now.AddYears(-2))).ToList();
3 years ago
Console.WriteLine();
3 years ago
//删除数据只在最近3张表执行操作
3 years ago
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
3 years ago
Console.WriteLine();
3 years ago
3 years ago
var tableName = db.SplitHelper<OrderSpliteTest>().GetTableName(DateTime.Now.AddDays(-1));
3 years ago
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();
3 years ago
var x2 = db.Updateable<OrderSpliteTest>()
.SetColumns(it=>it.Name=="a")
.Where(it => it.Pk == Guid.NewGuid())
3 years ago
.SplitTable(tabs => tabs.InTableNames(tableName2))
3 years ago
.ExecuteCommand();
3 years ago
3 years ago
Console.WriteLine();
3 years ago
//按日分表
3 years ago
var x3 = db.Insertable(new OrderSpliteTest() { Name="A" }).SplitTable().ExecuteCommand();
3 years ago
Console.WriteLine();
3 years ago
////强制分表类型
3 years ago
var x4 = db.Insertable(new OrderSpliteTest() { Name = "A" ,Time=DateTime.Now.AddDays(-1) }).SplitTable().ExecuteCommand();
Console.WriteLine("#### CodeFirst end ####");
}
3 years ago
[SplitTable(SplitType.Day)]
3 years ago
[SqlSugar.SugarTable("Taxxx0101_{year}{month}{day}")]
public class OrderSpliteTest
{
[SugarColumn(IsPrimaryKey =true)]
public Guid Pk{ get; set; }
public string Name { get; set; }
3 years ago
[SugarColumn(IsNullable =true)]
3 years ago
[SplitField]
3 years ago
public DateTime Time { get; set; }
}
}
}