- 将 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 读取数据库配置
81 lines
2.7 KiB
C#
81 lines
2.7 KiB
C#
using Avalonia;
|
||
using Avalonia_Common.Infrastructure;
|
||
using Avalonia_EFCore.Database;
|
||
using Avalonia_PC.Views;
|
||
using Avalonia_Services.Core;
|
||
using Avalonia_Services.Endpoints;
|
||
using Avalonia_Services.Services;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Serilog;
|
||
using System;
|
||
|
||
namespace Avalonia_PC
|
||
{
|
||
internal sealed class Program
|
||
{
|
||
public static IServiceProvider Services { get; private set; } = null!;
|
||
|
||
[STAThread]
|
||
public static void Main(string[] args)
|
||
{
|
||
// 初始化日志系统
|
||
AppLog.Initialize(LoggingConfiguration.CreateDefaultLogger(logDir: "logs"));
|
||
|
||
AppLog.Information("Avalonia-PC 正在启动...");
|
||
|
||
ConfigureServices();
|
||
|
||
// 初始化数据库(自动迁移 + 种子数据)
|
||
Services.InitializeDatabase<AppDataContext>();
|
||
|
||
// 启动时打印所有拦截的接口
|
||
var endpoints = Services.GetRequiredService<ServiceEndpointCollection>();
|
||
EndpointPrinter.PrintEndpoints(endpoints, "Avalonia-PC 拦截接口列表");
|
||
|
||
#if DEBUG
|
||
// 开启 WebView2 远程调试,启动后在 Edge 中访问 edge://inspect 调试网页
|
||
Environment.SetEnvironmentVariable(
|
||
"WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS",
|
||
"--remote-debugging-port=9222 --auto-open-devtools-for-tabs");
|
||
#endif
|
||
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
||
}
|
||
|
||
private static void ConfigureServices()
|
||
{
|
||
var services = new ServiceCollection();
|
||
|
||
// ---- 数据库 ----
|
||
// 注册默认数据库提供程序(SQLite / MySQL / PostgreSQL / SqlServer)
|
||
DatabaseProviderRegistry.RegisterDefaults();
|
||
|
||
// 桌面端固定使用 SQLite 本地数据库
|
||
services.AddAppDatabase<AppDataContext>(DatabaseConfiguration.ForSQLite("app.db"));
|
||
|
||
// ---- 业务服务 ----
|
||
services.AddSingleton<WeatherForecastService>();
|
||
|
||
// ---- 统一端点 ----
|
||
var endpointBuilder = new ServiceEndpointBuilder();
|
||
AppEndpoints.Configure(endpointBuilder);
|
||
var endpoints = endpointBuilder.Build();
|
||
services.AddSingleton(endpoints);
|
||
|
||
// 注册 Window
|
||
services.AddTransient<MainWindow>(sp => new MainWindow(sp));
|
||
|
||
Services = services.BuildServiceProvider();
|
||
}
|
||
|
||
// Avalonia configuration, don't remove; also used by visual designer.
|
||
public static AppBuilder BuildAvaloniaApp()
|
||
=> AppBuilder.Configure<App>()
|
||
.UsePlatformDetect()
|
||
#if DEBUG
|
||
.WithDeveloperTools()
|
||
#endif
|
||
.WithInterFont()
|
||
.LogToTrace();
|
||
}
|
||
}
|