Galosys.Foundation.ShardingCore
26.5.19.1
dotnet add package Galosys.Foundation.ShardingCore --version 26.5.19.1
NuGet\Install-Package Galosys.Foundation.ShardingCore -Version 26.5.19.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Galosys.Foundation.ShardingCore" Version="26.5.19.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.ShardingCore" Version="26.5.19.1" />
<PackageReference Include="Galosys.Foundation.ShardingCore" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Galosys.Foundation.ShardingCore --version 26.5.19.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Galosys.Foundation.ShardingCore, 26.5.19.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Galosys.Foundation.ShardingCore@26.5.19.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Galosys.Foundation.ShardingCore&version=26.5.19.1
#tool nuget:?package=Galosys.Foundation.ShardingCore&version=26.5.19.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Galosys.Foundation.ShardingCore
ShardingCore 分表模块,基于 ShardingCore 封装,为 EF Core 提供按时间维度的自动分表能力。
成熟度: :yellow_circle: 预发布 — 核心功能已实现,部分功能存在已知问题,新项目即将投入使用
快速开始
1. 定义分片实体
[Table("orders")]
[Sharding(ShardingType.Month)] // 按月分表:orders_202604、orders_202605...
public class Order : FullEntity<long>
{
public string OrderNo { get; set; }
}
分片粒度 (ShardingType 枚举):
| 粒度 | 表名后缀 | 示例 | 适用场景 |
|---|---|---|---|
Year |
_yyyy |
orders_2026 |
低频数据、历史归档 |
Month |
_yyyyMM |
orders_202604 |
订单、日志(推荐) |
Day |
_yyyyMMdd |
orders_20260415 |
高频数据、实时日志 |
2. 创建分片 DbContext
[DbContext("DefaultConnection", Dynamic = true)] // Dynamic = true 启用分表
public class AppDbContext : ShardingDbContext<AppDbContext>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
3. 注册服务
// Program.cs / Startup.cs
services.AddDbContext(configuration);
4. 手动建表(推荐)
由于 ShardingCore 自动建表功能存在已知问题,建议在应用启动时手动创建当前周期的物理表:
// 在应用启动后调用一次
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.EnsureShardingTablesAsync();
}
EnsureShardingTablesAsync会扫描所有标记了[Sharding]的实体,为当前周期创建物理表(CREATE TABLE IF NOT EXISTS语义,表已存在不报错)。
5. 正常使用
// 添加 — 自动路由到当月分表
await repository.AddAsync(new Order { OrderNo = "ORD-001" });
await repository.SaveAsync();
// 查询 — 自动路由并过滤
var orders = await repository.Query(o => o.OrderNo.StartsWith("ORD")).ToListAsync();
缓存策略
DynamicModelCacheKeyFactory 根据分片粒度优化模型缓存:
- 按年分片: 模型缓存每年重建一次(缓存 key 包含年初日期)
- 按月分片: 模型缓存每月重建一次(缓存 key 包含月初日期)
- 按天分片: 模型缓存每天重建一次(缓存 key 包含当天日期)
- 混合粒度: 取最细粒度(Day > Month > Year)
例如:DbContext 中有按月和按年两种分片实体,模型缓存按月重建。
已知问题
| 问题 | 状态 | 说明 |
|---|---|---|
| 自动建表 HostedService 报错 | ❌ 未修复 | ShardingCore 库限制,使用 EnsureShardingTablesAsync 替代 |
| 审计字段与路由评估时序冲突 | ✅ 已修复 (v3) | 审计填充已移至 SaveChangesAsync 重写中,确保路由评估后执行 |
配置参考
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=MyApp;Trusted_Connection=True;" // 基础连接串
}
}
// [DbContext] 特性配置
[DbContext("DefaultConnection", Dynamic = true, DbVersion = "8.0.34")]
// ↑ 连接串名称 ↑ 启用分表 ↑ 数据库版本(MySQL)
public class AppDbContext : ShardingDbContext<AppDbContext> { }
依赖
Galosys.Foundation.EntityFrameworkCore— EF Core 基础模块Galosys.Foundation.Core— 核心库ShardingCore— 分表核心库
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Galosys.Foundation.Core (>= 26.5.19.1)
- Galosys.Foundation.EntityFrameworkCore (>= 26.5.19.1)
- shardingcore (>= 7.8.1.21)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 26.5.19.1 | 37 | 5/19/2026 |
| 26.5.18.1 | 85 | 5/18/2026 |
| 26.5.15.1 | 84 | 5/15/2026 |
| 26.5.12.3 | 90 | 5/12/2026 |
| 26.5.12.2 | 86 | 5/12/2026 |
| 26.4.27.1-rc1 | 94 | 4/26/2026 |
| 26.4.25.1-rc1 | 84 | 4/25/2026 |
| 26.4.22.2-rc7 | 90 | 4/22/2026 |
| 26.4.22.2-rc6 | 85 | 4/22/2026 |
| 26.4.22.2-rc4 | 87 | 4/22/2026 |
| 26.4.22.2-rc3 | 86 | 4/22/2026 |
| 26.4.12.8-rc1 | 98 | 4/12/2026 |
| 26.4.12.7-rc1 | 96 | 4/12/2026 |
| 26.1.30.1-rc1 | 116 | 1/30/2026 |
| 26.1.29.1 | 113 | 1/29/2026 |
| 26.1.28.5 | 111 | 1/28/2026 |
| 26.1.28.4 | 109 | 1/28/2026 |
| 26.1.28.2 | 107 | 1/28/2026 |
| 26.1.23.6 | 109 | 1/23/2026 |
| 26.1.21.1 | 109 | 1/21/2026 |
Loading failed