feat: 将数据库模型和迁移集中到 EFCore 项目
- 将 AppDataContext、实体模型、Migrations 从 Avalonia-Services 移动到 Avalonia-EFCore - 更新 API、PC、Services 中的数据库上下文和实体引用命名空间 - 在实体上显式绑定表名、字段名和数据库注释 - 更新 InitialCreate、Designer、Snapshot,使用新的表名、字段名和注释 - 新增 AppDataContextFactory,支持 dotnet ef 设计时创建 DbContext - 新增本地 dotnet-ef 工具清单 - 新增一键生成迁移脚本 add-migration.ps1 / .cmd / .bat - 启动时自动检测并执行未应用迁移 - 从 appsettings.json 读取数据库配置
This commit is contained in:
@@ -9,6 +9,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using Avalonia_EFCore.Database;
|
||||
using Avalonia_Services.Core;
|
||||
using Avalonia_Services.Database;
|
||||
using Avalonia_Services.Endpoints;
|
||||
using Avalonia_Services.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Avalonia_API.Configuration
|
||||
@@ -13,15 +13,20 @@ namespace Avalonia_API.Configuration
|
||||
/// 注册统一端点及其依赖的服务(含数据库)。
|
||||
/// 所有业务端点定义在 Avalonia-Services/Endpoints/AppEndpoints.cs。
|
||||
/// </summary>
|
||||
public static IServiceCollection AddUnifiedApiServices(this IServiceCollection services)
|
||||
public static IServiceCollection AddUnifiedApiServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
// ---- 数据库 ----
|
||||
// 从 appsettings.json 读取 DatabaseConfiguration 节
|
||||
// 注册默认数据库提供程序(SQLite / MySQL / PostgreSQL / SqlServer)
|
||||
DatabaseProviderRegistry.RegisterDefaults();
|
||||
|
||||
var databaseConfig = configuration
|
||||
.GetSection(nameof(DatabaseConfiguration))
|
||||
.Get<DatabaseConfiguration>()
|
||||
?? DatabaseConfiguration.ForSQLite("app.db");
|
||||
|
||||
// 注册 AppDataContext(共享数据上下文)
|
||||
services.AddAppDatabase<AppDataContext>(DatabaseConfiguration.ForSQLite("app.db"));
|
||||
services.AddAppDatabase<AppDataContext>(databaseConfig);
|
||||
|
||||
// ---- 业务服务 ----
|
||||
services.AddScoped<WeatherForecastService>();
|
||||
|
||||
@@ -3,7 +3,6 @@ using Avalonia_API.Extensions;
|
||||
using Avalonia_Common.Infrastructure;
|
||||
using Avalonia_EFCore.Database;
|
||||
using Avalonia_Services.Core;
|
||||
using Avalonia_Services.Database;
|
||||
using Serilog;
|
||||
|
||||
// 初始化日志系统
|
||||
@@ -22,7 +21,7 @@ try
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
// 注册统一端点及业务服务(入口在 Avalonia-Services/Endpoints/AppEndpoints.cs)
|
||||
builder.Services.AddUnifiedApiServices();
|
||||
builder.Services.AddUnifiedApiServices(builder.Configuration);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
"Provider": "SQLite",
|
||||
"ConnectionString": "Data Source=avalonia-api.db",
|
||||
"AutoMigrate": true,
|
||||
"RecreateDatabase": false,
|
||||
"EnableDetailedLog": false,
|
||||
"Timeout": 30
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user