- 将 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 读取数据库配置
66 lines
3.4 KiB
C#
66 lines
3.4 KiB
C#
using System;
|
||
using Microsoft.EntityFrameworkCore.Metadata;
|
||
using Microsoft.EntityFrameworkCore.Migrations;
|
||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||
|
||
#nullable disable
|
||
|
||
namespace Avalonia_EFCore.Migrations
|
||
{
|
||
/// <summary>
|
||
/// 初始数据库基线。后续软件版本只追加新的 Migration,不修改已发布 Migration。
|
||
/// </summary>
|
||
public partial class InitialCreate : Migration
|
||
{
|
||
protected override void Up(MigrationBuilder migrationBuilder)
|
||
{
|
||
migrationBuilder.CreateTable(
|
||
name: "user",
|
||
columns: table => new
|
||
{
|
||
Id = table.Column<int>(name: "id", nullable: false, comment: "用户主键")
|
||
.Annotation("SqlServer:Identity", "1, 1")
|
||
.Annotation("Sqlite:Autoincrement", true)
|
||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)
|
||
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
|
||
Name = table.Column<string>(name: "name", maxLength: 100, nullable: true, comment: "用户名称"),
|
||
Email = table.Column<string>(name: "email", maxLength: 200, nullable: true, comment: "用户邮箱"),
|
||
CreatedAt = table.Column<DateTime>(name: "created-at", nullable: false, comment: "创建时间"),
|
||
UpdatedAt = table.Column<DateTime>(name: "updated-at", nullable: false, comment: "更新时间")
|
||
},
|
||
constraints: table =>
|
||
{
|
||
table.PrimaryKey("pk-user", x => x.Id);
|
||
},
|
||
comment: "用户实体,演示数据库 CRUD 操作");
|
||
|
||
migrationBuilder.CreateTable(
|
||
name: "weather-forecast",
|
||
columns: table => new
|
||
{
|
||
Id = table.Column<int>(name: "id", nullable: false, comment: "天气预报主键")
|
||
.Annotation("SqlServer:Identity", "1, 1")
|
||
.Annotation("Sqlite:Autoincrement", true)
|
||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)
|
||
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
|
||
Date = table.Column<DateOnly>(name: "date", nullable: false, comment: "预报日期"),
|
||
TemperatureC = table.Column<int>(name: "temperature-c", nullable: false, comment: "摄氏温度"),
|
||
Summary = table.Column<string>(name: "summary", maxLength: 200, nullable: true, comment: "天气摘要"),
|
||
CreatedAt = table.Column<DateTime>(name: "created-at", nullable: false, comment: "创建时间"),
|
||
UpdatedAt = table.Column<DateTime>(name: "updated-at", nullable: false, comment: "更新时间")
|
||
},
|
||
constraints: table =>
|
||
{
|
||
table.PrimaryKey("pk-weather-forecast", x => x.Id);
|
||
},
|
||
comment: "天气预报数据实体");
|
||
}
|
||
|
||
protected override void Down(MigrationBuilder migrationBuilder)
|
||
{
|
||
migrationBuilder.DropTable(name: "weather-forecast");
|
||
migrationBuilder.DropTable(name: "user");
|
||
}
|
||
}
|
||
}
|