sqlsugar 按年分表
时间: 2024-02-04 18:09:32 浏览: 227
以下是使用SqlSugar按年分表的示例代码:
```csharp
// 配置分表规则
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "your_connection_string",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = new HttpRuntimeCache()
},
MoreSettings = new ConnMoreSettings()
{
IsAutoRemoveDataCache = true
}
});
// 创建实体类
public class Order
{
public int Id { get; set; }
public string OrderNo { get; set; }
public DateTime CreateTime { get; set; }
}
// 按年分表
db.MappingTables.Add("Order_{0}", d => d.CreateTime.Year);
db.CodeFirst.InitTables(typeof(Order));
// 插入数据
Order order = new Order()
{
OrderNo = "20210001",
CreateTime = DateTime.Now
};
db.Insertable(order).SplitTable().ExecuteCommand();
// 查询数据
var orders = db.Queryable<Order>().SplitTable().ToList();
// 相关问题:
阅读全文