/database-sqlserver:syncing-db-entitiesfrom database-sqlserver
Use when adding new DB tables/views to an EF Core project, syncing entity changes from the database, fixing Entity precision / nullable / MaxLength that drifted from the DB actual schema, or user says "加表", "同步 Entity", "scaffold", "efcpt", "更新實體", "re-scaffold", "重新產生 Entity". Also trigger when reading a file with header `// <auto-generated> This file has been auto generated by EF Core Power Tools.` that needs modification — manual edits will be overwritten, must go through efcpt.
Ships in database-sqlserver. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
用 EF Core Power Tools(efcpt)從資料庫 reverse engineer Entity,再建立 EF Core Migration 的完整流程。
核心原則:Entity 是資料庫 schema 的鏡射。修 Entity 的 precision / nullable / MaxLength 不要手改 auto-generated 檔(會被下次 scaffold 覆蓋),一律回到 efcpt 重新產生。
當本次變更需要新增/改 schema(新表、加欄、view 改名、型別變)時,務必照順序:先讓來源資料庫具備目標 schema → efcpt re-scaffold → 才建 migration。
🚫 絕不 migration-first(先建 migration 再 scaffold 的兩個坑):
migrations add 吸入髒 snapshot。正解:efcpt 改名完成才 migrations add(見 Task 5)。若專案的 reverse-engineer 來源是一個獨立的「設計庫 / 參考庫」(與 runtime/app DB 不同的連線),要先在那個庫套用 schema 變更(並備妥 rollback plan),再 scaffold。設計庫已有目標 schema、只是 re-scaffold → 直接走下方 Task 1–5。
CLI 與 GUI 使用不同的 config 檔案,兩邊必須同步:
| GUI | CLI | |
|---|---|---|
| Config | efpt.config.json | efcpt-config.json |
| 格式 | flat JSON(UseT4) | nested JSON(use-t4) |
| 表清單 | Tables[].Name + ObjectType | tables[].name + views[].name(分開) |
新增表時必須同時更新兩個 config。
進入 Task 1 之前,先檢查目標專案目錄同時含兩個 config:
find <project-dir> -name "efpt.config.json" -o -name "efcpt-config.json"
efpt.config.json(常見於 legacy 專案)→ 需補新版 CLI config。可手動改寫:JSON key 由 capitalized 轉 kebab-case,Tables 按 ObjectType 分為 tables(0)與 views(3)。efcpt-config.json → 本流程僅需新版 CLI config;舊版僅 VS GUI 使用者才需要,可省略。必須使用與專案 EF Core 版本對應的 CLI,版本不符會造成 Entity 屬性名稱差異、build 失敗:
# 專案用 EF Core 8 → CLI 用 8.x
dotnet tool install ErikEJ.EFCorePowerTools.Cli -g --version 8.*
efcpt --version # 確認顯示 "for EF Core 8"
| 專案 EF Core | CLI 版本指令 |
|---|---|
| 8.x | --version 8.* |
| 9.x | --version 9.* |
| 10.x | 預設(最新) |
flowchart TD
S0{本次需<br/>新增/改 schema?}
DBF[DB-first 前置:先讓來源庫具備目標 schema<br/>🚫 絕不 migration-first]
T0[Task 0: 偵測雙 config<br/>efpt.config.json + efcpt-config.json]
T0CHK{雙 config<br/>狀態?}
T0FIX[補齊缺失版本]
T1[Task 1: 確認目標表<br/>+ 取設計庫連線字串]
T2[Task 2: 更新雙 config<br/>📌 兩邊同步加表/欄位]
T3[Task 3: efcpt CLI scaffold<br/>產生 auto-gen Entity + Configuration]
T4[Task 4: 檢查產出 + dotnet build<br/>驗證無編譯錯誤]
T5[Task 5: 建立 Migration]
END([Entity 同步完成])
S0 -->|是,需改 schema| DBF --> T0
S0 -->|否,純 re-scaffold| T0
T0 --> T0CHK
T0CHK -->|只有舊版 efpt| T0FIX --> T1
T0CHK -->|只有新版 efcpt| T1
T0CHK -->|兩邊不同步| T0FIX
T0CHK -->|齊全| T1
T1 --> T2 --> T3 --> T4 --> T5 --> END
classDef sync fill:#fff9c4,stroke:#f57f17,color:#000
classDef danger fill:#ffcdd2,stroke:#c62828,color:#000
class T0CHK sync
class T2 danger
class DBF danger
核心原則:CLI(efcpt-config.json)+ GUI(efpt.config.json)雙 config 必須同步表清單,否則 VS GUI 操作或 CLI scaffold 任一邊會抓不到表。
向使用者確認:
[Production].[NewTable]efcpt 需要一個 live 資料庫連線來讀取 schema。組成標準連線字串:
Server=<host>,<port>;Database=<db>;User Id=<user>;Password=<pw>;TrustServerCertificate=True;
用你慣用的 DB 查詢工具先確認表存在且結構正確(欄位、型別、nullable)。
連線字串絕對不寫入 config 或 commit,只在記憶體或 shell 變數內存在。
必須同時更新兩個檔案,排序規則:按 Schema 分組,Schema 內按名稱字母排序。
{
"Name": "[Schema].[TableName]",
"ObjectType": 0
}
ObjectType:0 = Table、3 = View
Table 加在 "tables" 陣列,View 加在 "views" 陣列:
{
"name": "[Schema].[TableName]"
}
驗證:兩個 config 的表清單一致。
efcpt --version # 必須顯示對應的 EF Core 版本
cd src/YourApp.Domain
efcpt "<Task 1 組合的連線字串>" mssql
CLI 自動讀取 efcpt-config.json 和 CodeTemplates/EFCore/*.t4(若有)。
| CLI 設定 | GUI 設定 | 範例值 |
|---|---|---|
use-t4 | UseT4 | true |
use-data-annotations | UseFluentApiOnly=false | true |
output-path | OutputPath | "Entities" |
output-dbcontext-path | OutputContextPath | "Data" |
use-schema-folders-preview | UseSchemaFolders | true |
split-dbcontext-preview | UseDbContextSplitting | true |
use-DateOnly-TimeOnly | UseDateOnlyTimeOnly | false |
dbcontext-name | ContextClassName | "AppDbContext" |
refresh-object-lists | N/A | false(防止抓全部表) |
驗證:scaffold 成功,新 Entity 在 Entities/{Schema}/ 下。
Entities/{Schema}/{Entity}.cs — EntityData/Configurations/{Entity}Configuration.cs — ConfigurationData/AppDbContext.cs — 新增 DbSet<Entity>git diff --stat src/YourApp.Domain/
除了 AppDbContext.cs 和兩個 config 外,不應有其他既有檔案被修改。
dotnet build YourApp.sln -c Release --no-restore
驗證:build 成功,既有檔案零差異。
接 ef-migration skill 處理 migration 建立 / 套用 / 失敗修復:
EF_ARGS="--project src/YourApp.Domain --startup-project src/YourApp.Domain"
dotnet ef migrations add Add{Entity}Table $EF_ARGS --context AppDbContext
dotnet build YourApp.sln -c Release --no-restore
命名慣例:Add{Entity}Table、Add{Entity}{Field}、Alter{Entity}{Field}。
驗證:Migration 的 Up() / Down() 正確、build 成功。
| 想法 | 現實 |
|---|---|
「用 dotnet ef dbcontext scaffold 就好」 | 必須用 efcpt CLI,才能讀取 config 與 T4 模板 |
| 「只改一個 config 就好」 | 必須同時更新 efpt.config.json 和 efcpt-config.json(雙 config 同步) |
| 「裝最新版 CLI 就好」 | 必須與專案 EF Core 版本一致,否則 Entity 差異會破壞 build |
| 「連線字串寫進 config 或 commit」 | 禁止,僅在記憶體或 shell 變數內使用 |
| 「efcpt 連的就是 runtime DB」 | 若專案用獨立設計庫,efcpt 連的是設計庫,不是 app runtime DB |
| 「scaffold 完就好,不用檢查」 | 必須確認既有檔案 zero-diff 再建 migration(已存在的 Entity/Configuration 會被覆蓋,有手動修改需先備份) |
「直接跑 database update」 | 先檢查 migration SQL,確認無破壞性變更 |
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub kerryhuang/kerryhuang-plugins --plugin database-sqlserver6 skills in this plugin
/ef-migrationUse when 建立 / 套用 / 回復 EF Core migration、跑 `dotnet ef` 指令、切換目標資料庫連線、migration 失敗診斷。管理 SQL Server 上的 Entity Framework Core 資料庫 Migration。觸發:「建 migration」「套用 migration」「回滾 migration」「dotnet ef」「migration 失敗」。/generating-table-descriptionUse when generating table description documentation from SQL view files. Use when user says "產生資料表描述", "文件化檢視表", "更新 TableDescription", "由 view 產欄位對照". Use when user wants to document view-to-table field mappings for a SQL Server database./migration-troubleshootUse when diagnosing special EF Core migration situations — ModelSnapshot 衝突、歷史紀錄遺失、資料庫已套用但 migration 對不上、生產緊急回滾判斷。一般失敗修復循環走 ef-migration skill。觸發:「migration 對不上」「snapshot 衝突」「緊急回滾」「migration 診斷」。Other plugins' skills for the same job