分表测试
程序
using SqlSugar;
var db = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = "Server=127.0.0.1;Database=test;User ID=sa;Password=123456;Trusted_Connection=False;TrustServerCertificate=True;",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
});
db.CodeFirst.InitTables(typeof(SysUser));
for (int i = 0; i < 10; i++)
{
var newUser = new SysUser
{
Id = SnowFlakeSingle.instance.NextId(),
Name = "John Doe",
};
db.Insertable(newUser).SplitTable().ExecuteCommand();
}
var users = db.Queryable<SysUser>().SplitTable(tabs => tabs.Take(3)).ToList();
var i = db.MasterQueryable<SysUser>().Where(it => it.Name == "John Doe").SplitTable(tabs => tabs.Take(2)).Select(s => SqlFunc.AggregateDistinctCount(s.Id)).First();
测试表类
using SqlSugar;
[SplitTable(SplitType.Day)]
[SugarTable("UserSplit{year}{month}{day}")]
public class SysUser
{
[Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="id" ,IsPrimaryKey = true )]
public long Id { get; set; }
[SugarColumn(ColumnName="cardNo",IsNullable =true)]
public string? CardNo { get; set; }
[SugarColumn(ColumnName="userNo" , IsNullable = true)]
public string? UserNo { get; set; }
[SugarColumn(ColumnName="name")]
public string? Name { get; set; }
[SplitField]
public DateTime CreateTime { get; set; } = DateTime.Now;
}