为 14 个项目中缺少 XML 注释的类、接口、方法、属性、字段、record、 枚举等成员补全中文文档注释。接口方法在接口层定义完整注释,实现类 使用 <inheritdoc /> 引用。私有辅助方法结合业务语义编写注释。 扫描结果:missing-csharp-docs.txt 缺失项归零。 构建结果:0 警告,0 错误。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
92 lines
3.7 KiB
C#
92 lines
3.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace FileShare_EFCore.Database
|
|
{
|
|
/// <summary>
|
|
/// 数据库服务注册扩展 —— 在 Program.cs 中一行配置数据库。
|
|
/// </summary>
|
|
public static class DatabaseExtensions
|
|
{
|
|
/// <summary>
|
|
/// 注册数据库上下文及相关服务。
|
|
/// </summary>
|
|
/// <typeparam name="TContext">继承自 AppDbContext 的业务 DbContext</typeparam>
|
|
public static IServiceCollection AddAppDatabase<TContext>(
|
|
this IServiceCollection services,
|
|
DatabaseConfiguration config)
|
|
where TContext : AppDbContext
|
|
{
|
|
// 注册配置
|
|
services.AddSingleton(config);
|
|
|
|
if (typeof(TContext) == typeof(AppDataContext))
|
|
{
|
|
services.AddProviderAppDataContext(config);
|
|
services.AddScoped<DatabaseManager<TContext>>();
|
|
|
|
return services;
|
|
}
|
|
|
|
// 注册 DbContext
|
|
services.AddDbContext<TContext>(options =>
|
|
{
|
|
AppDbContext.ConfigureProvider(options, config);
|
|
});
|
|
|
|
// 注册数据库管理器
|
|
services.AddScoped<DatabaseManager<TContext>>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据 <see cref="DatabaseConfiguration.Provider"/> 注册对应具体类型的 <see cref="AppDataContext"/> 实现。
|
|
/// </summary>
|
|
/// <param name="services">服务集合。</param>
|
|
/// <param name="config">数据库配置。</param>
|
|
/// <exception cref="NotSupportedException">数据库提供程序未注册时抛出。</exception>
|
|
private static void AddProviderAppDataContext(this IServiceCollection services, DatabaseConfiguration config)
|
|
{
|
|
switch (config.Provider)
|
|
{
|
|
case DatabaseProvider.SQLite:
|
|
services.AddDbContext<AppDataContext, SqliteAppDataContext>(options =>
|
|
AppDbContext.ConfigureProvider(options, config));
|
|
break;
|
|
case DatabaseProvider.SqlServer:
|
|
services.AddDbContext<AppDataContext, SqlServerAppDataContext>(options =>
|
|
AppDbContext.ConfigureProvider(options, config));
|
|
break;
|
|
case DatabaseProvider.PostgreSQL:
|
|
services.AddDbContext<AppDataContext, PostgreSqlAppDataContext>(options =>
|
|
AppDbContext.ConfigureProvider(options, config));
|
|
break;
|
|
case DatabaseProvider.MySQL:
|
|
services.AddDbContext<AppDataContext, MySqlAppDataContext>(options =>
|
|
AppDbContext.ConfigureProvider(options, config));
|
|
break;
|
|
default:
|
|
throw new NotSupportedException($"数据库提供程序 {config.Provider} 未注册。");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化数据库(在应用启动时调用一次)。
|
|
/// </summary>
|
|
public static IServiceProvider InitializeDatabase<TContext>(
|
|
this IServiceProvider serviceProvider,
|
|
Action<TContext, IServiceProvider?>? seeder = null)
|
|
where TContext : AppDbContext
|
|
{
|
|
using var scope = serviceProvider.CreateScope();
|
|
var dbManager = scope.ServiceProvider.GetRequiredService<DatabaseManager<TContext>>();
|
|
|
|
// 同步等待初始化(启动时阻塞)
|
|
dbManager.InitializeAsync(seeder).GetAwaiter().GetResult();
|
|
|
|
return serviceProvider;
|
|
}
|
|
}
|
|
}
|