- 将 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 读取数据库配置
37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using Avalonia_EFCore.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Avalonia_EFCore.Database
|
|
{
|
|
/// <summary>
|
|
/// 应用数据库上下文 —— 继承自 Avalonia-EFCore 的 AppDbContext。
|
|
/// 所有业务实体在此注册 DbSet。
|
|
/// 这是 Avalonia-API 和 Avalonia-PC 共用的具体数据上下文。
|
|
/// </summary>
|
|
public class AppDataContext(DatabaseConfiguration dbConfig) : AppDbContext(dbConfig)
|
|
{
|
|
/// <summary>天气预报数据</summary>
|
|
public DbSet<WeatherForecastEntity> WeatherForecasts => Set<WeatherForecastEntity>();
|
|
|
|
/// <summary>用户数据</summary>
|
|
public DbSet<UserEntity> Users => Set<UserEntity>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.Entity<WeatherForecastEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id).HasName("pk-weather-forecast");
|
|
entity.Property(e => e.Summary).HasMaxLength(200);
|
|
});
|
|
|
|
modelBuilder.Entity<UserEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id).HasName("pk-user");
|
|
entity.Property(e => e.Email).HasMaxLength(200);
|
|
});
|
|
}
|
|
}
|
|
}
|