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:
@@ -1,37 +0,0 @@
|
||||
using Avalonia_EFCore.Database;
|
||||
using Avalonia_Services.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Avalonia_Services.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);
|
||||
entity.Property(e => e.Summary).HasMaxLength(200);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<UserEntity>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.Email).HasMaxLength(200);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using Avalonia_Common.Core;
|
||||
using Avalonia_EFCore.Database;
|
||||
using Avalonia_EFCore.Models;
|
||||
using Avalonia_Services.Core;
|
||||
using Avalonia_Services.Database;
|
||||
using Avalonia_Services.Models;
|
||||
using Avalonia_Services.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Avalonia_Services.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户实体 —— 演示数据库 CRUD 操作。
|
||||
/// </summary>
|
||||
[Table("Users")]
|
||||
public class UserEntity
|
||||
{
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
|
||||
[MaxLength(100)]
|
||||
public string? Name { get; set; }
|
||||
|
||||
[MaxLength(200)]
|
||||
public string? Email { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
|
||||
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace Avalonia_Services.Models
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Avalonia_Services.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// 天气预报数据实体 —— 对应数据库中的 WeatherForecasts 表。
|
||||
/// </summary>
|
||||
[Table("WeatherForecasts")]
|
||||
public class WeatherForecastEntity
|
||||
{
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
[MaxLength(200)]
|
||||
public string? Summary { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
|
||||
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using Avalonia_Services.Models;
|
||||
using Avalonia_EFCore.Models;
|
||||
|
||||
namespace Avalonia_Services.Services
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user