2026-07-02 15:53:30 +08:00
|
|
|
|
# DbContext 工厂
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
> 所属模块:[数据库支持](./概览.md)
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
## 1. 本地库工厂
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
```csharp
|
2026-08-13 15:01:32 +08:00
|
|
|
|
// Infrastructure/LocalData/IDbContextFactory.cs
|
|
|
|
|
|
namespace Gpulse.WCT.DataAnalyzer.Core.Infrastructure.LocalData;
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
public interface IDbContextFactory
|
|
|
|
|
|
{
|
|
|
|
|
|
WctMinerDbContext CreateDbContext();
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
工厂实现读取 `Database:LocalPath`(fallback `Database:Path`),支持 SQLite 和 PostgreSQL。
|
|
|
|
|
|
|
2026-07-02 15:53:30 +08:00
|
|
|
|
---
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
## 2. 发布库工厂
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
```csharp
|
2026-08-13 15:01:32 +08:00
|
|
|
|
// Infrastructure/ReleaseData/IReleaseDbContextFactory.cs
|
|
|
|
|
|
namespace Gpulse.WCT.DataAnalyzer.Core.Infrastructure.ReleaseData;
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
public interface IReleaseDbContextFactory
|
2026-07-02 15:53:30 +08:00
|
|
|
|
{
|
2026-08-13 15:01:32 +08:00
|
|
|
|
ReleaseDbContext CreateDbContext();
|
2026-07-02 15:53:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
工厂实现读取 `Database:ReleasePath`,支持 SQLite 和 PostgreSQL。PostgreSQL 时使用 `Release*` 前缀配置(`ReleaseHost`, `ReleasePort`, `ReleaseName`, `ReleaseUser`, `ReleasePassword`),未设置时回退到 local 的对应配置。
|
|
|
|
|
|
|
2026-07-02 15:53:30 +08:00
|
|
|
|
---
|
|
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
## 3. DI 注册
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
|
|
|
|
|
```csharp
|
2026-08-13 15:01:32 +08:00
|
|
|
|
// Infrastructure/Extensions/ServiceCollectionExtensions.cs
|
|
|
|
|
|
public static IServiceCollection AddDatabaseServices(
|
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
|
IConfiguration configuration)
|
2026-07-02 15:53:30 +08:00
|
|
|
|
{
|
2026-08-13 15:01:32 +08:00
|
|
|
|
// 本地库
|
|
|
|
|
|
services.AddScoped<IDbContextFactory, DbContextFactory>();
|
|
|
|
|
|
services.AddScoped<WctMinerDbContext>(sp =>
|
2026-07-02 15:53:30 +08:00
|
|
|
|
{
|
2026-08-13 15:01:32 +08:00
|
|
|
|
var factory = sp.GetRequiredService<IDbContextFactory>();
|
|
|
|
|
|
return factory.CreateDbContext();
|
|
|
|
|
|
});
|
2026-07-02 15:53:30 +08:00
|
|
|
|
|
2026-08-13 15:01:32 +08:00
|
|
|
|
// 发布库(独立)
|
|
|
|
|
|
services.AddScoped<IReleaseDbContextFactory, ReleaseDbContextFactory>();
|
|
|
|
|
|
services.AddScoped<ReleaseDbContext>(sp =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var factory = sp.GetRequiredService<IReleaseDbContextFactory>();
|
|
|
|
|
|
return factory.CreateDbContext();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return services;
|
2026-07-02 15:53:30 +08:00
|
|
|
|
}
|
2026-08-13 15:01:32 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 4. 启动初始化
|
|
|
|
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
|
|
// Program.cs
|
|
|
|
|
|
using var scope = app.Services.CreateScope();
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化本地库
|
|
|
|
|
|
var dbContext = scope.ServiceProvider.GetRequiredService<WctMinerDbContext>();
|
|
|
|
|
|
await dbContext.Database.EnsureCreatedAsync();
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化发布库
|
|
|
|
|
|
var releaseContext = scope.ServiceProvider.GetRequiredService<ReleaseDbContext>();
|
|
|
|
|
|
await releaseContext.Database.EnsureCreatedAsync();
|
|
|
|
|
|
|
|
|
|
|
|
// 本地库种子数据
|
|
|
|
|
|
SeedData.Initialize(dbContext);
|
2026-07-02 15:53:30 +08:00
|
|
|
|
```
|