From 9f4c67d2fa7f1b426184dd14e752420367d9c511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E4=BC=9F?= Date: Sun, 7 Jun 2026 19:26:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...236\351\241\276\347\254\224\350\256\260.md" | 7 +++++++ ...233\345\273\272\351\241\271\347\233\256.md" | 18 ++++++++++++++++++ ...7\254\224\350\256\260-nuget\345\214\205.md" | 13 +++++++++++++ .../20260605\347\254\224\350\256\260-CRUD.md" | 18 ++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 "\351\202\223\344\274\237/20260601 C#\345\233\236\351\241\276\347\254\224\350\256\260.md" create mode 100644 "\351\202\223\344\274\237/20260603\347\254\224\350\256\260-\345\210\233\345\273\272\351\241\271\347\233\256.md" create mode 100644 "\351\202\223\344\274\237/20260604\347\254\224\350\256\260-nuget\345\214\205.md" create mode 100644 "\351\202\223\344\274\237/20260605\347\254\224\350\256\260-CRUD.md" diff --git "a/\351\202\223\344\274\237/20260601 C#\345\233\236\351\241\276\347\254\224\350\256\260.md" "b/\351\202\223\344\274\237/20260601 C#\345\233\236\351\241\276\347\254\224\350\256\260.md" new file mode 100644 index 0000000..47f6d9f --- /dev/null +++ "b/\351\202\223\344\274\237/20260601 C#\345\233\236\351\241\276\347\254\224\350\256\260.md" @@ -0,0 +1,7 @@ +## 笔记 + +安装SDK + +1.访问 https://dotnet.microsoft.com/download/dotnet/8.0 +2.下载 .NET 8.0 SDK +3.双击安装包,按提示完成 \ No newline at end of file diff --git "a/\351\202\223\344\274\237/20260603\347\254\224\350\256\260-\345\210\233\345\273\272\351\241\271\347\233\256.md" "b/\351\202\223\344\274\237/20260603\347\254\224\350\256\260-\345\210\233\345\273\272\351\241\271\347\233\256.md" new file mode 100644 index 0000000..bddc28e --- /dev/null +++ "b/\351\202\223\344\274\237/20260603\347\254\224\350\256\260-\345\210\233\345\273\272\351\241\271\347\233\256.md" @@ -0,0 +1,18 @@ +## 笔记 + +创建项目 + +在终端(PowerShell / bash)里: + + 切到你想放代码的目录:mkdir MyShopApi && cd MyShopApi + + 创建 Web API 项目:dotnet new webapi -n MyShopApi --use-controllers + + 进入项目目录:cd MyShopApi + + + webapi:项目模板名 + + -n MyShopApi:项目名(生成的目录名、命名空间) + + --use-controllers:用 Controller-based 风格(vs Minimal API) \ No newline at end of file diff --git "a/\351\202\223\344\274\237/20260604\347\254\224\350\256\260-nuget\345\214\205.md" "b/\351\202\223\344\274\237/20260604\347\254\224\350\256\260-nuget\345\214\205.md" new file mode 100644 index 0000000..297cbe4 --- /dev/null +++ "b/\351\202\223\344\274\237/20260604\347\254\224\350\256\260-nuget\345\214\205.md" @@ -0,0 +1,13 @@ +## 笔记 + +装 NuGet 包与 dotnet-ef 工具 + +安装NuGet 包 + + dotnet add package Microsoft.EntityFrameworkCore.Sqlite -v 8—— SQLite 数据库 provider + dotnet add package Microsoft.EntityFrameworkCore.Design-v 8—— 迁移命令依赖(dotnet ef) + +安装dotnet-ef 工具 + + dotnet tool install --global dotnet-ef + 验证:dotnet ef --version \ No newline at end of file diff --git "a/\351\202\223\344\274\237/20260605\347\254\224\350\256\260-CRUD.md" "b/\351\202\223\344\274\237/20260605\347\254\224\350\256\260-CRUD.md" new file mode 100644 index 0000000..571ba38 --- /dev/null +++ "b/\351\202\223\344\274\237/20260605\347\254\224\350\256\260-CRUD.md" @@ -0,0 +1,18 @@ +## 笔记 + + +CRUD改造 + +通过构造函数注入拿到AppDbContext实例: + + public class CategoriesController : ControllerBase + { + private readonly AppDbContext _db; + + public CategoriesController(AppDbContext db) + { + _db = db; + } + + // ... + } \ No newline at end of file -- Gitee From d44453db6a503722b0fc7e1610a4be50f0208fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E4=BC=9F?= Date: Sun, 14 Jun 2026 20:11:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...26\351\224\256\345\205\263\347\263\273.md" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "\351\202\223\344\274\237/20260608-webapi\344\270\255\347\232\204\344\270\273\345\244\226\351\224\256\345\205\263\347\263\273.md" diff --git "a/\351\202\223\344\274\237/20260608-webapi\344\270\255\347\232\204\344\270\273\345\244\226\351\224\256\345\205\263\347\263\273.md" "b/\351\202\223\344\274\237/20260608-webapi\344\270\255\347\232\204\344\270\273\345\244\226\351\224\256\345\205\263\347\263\273.md" new file mode 100644 index 0000000..78180ba --- /dev/null +++ "b/\351\202\223\344\274\237/20260608-webapi\344\270\255\347\232\204\344\270\273\345\244\226\351\224\256\345\205\263\347\263\273.md" @@ -0,0 +1,97 @@ +# 笔记 + +--- + +## 🧩 核心概念 + +| 概念 | 说明 | +|------|------| +| **一对多关系** | 一个实体(Category)对应多个另一个实体(Product) | +| **导航属性** | 实体间通过属性表达关系,而不是靠手动写 JOIN | +| **外键** | “多”端存储“一”端的主键值(如 `Product.CategoryId`) | + +--- + +## 🧱 实体配置模板 + +```csharp +// “一”端:Category +public ICollection Products { get; set; } + +// “多”端:Product +public int CategoryId { get; set; } +public Category? Category { get; set; } +``` + +**EF Core 约定识别规则**: +- 集合类型 → “一”端 +- 引用类型 → “多”端 +- `<类名>Id` → 外键 + +--- + +## ⚙️ `OnModelCreating` 显式配置(推荐) + +```csharp +modelBuilder.Entity() + .HasOne(p => p.Category) + .WithMany(c => c.Products) + .HasForeignKey(p => p.CategoryId) + .OnDelete(DeleteBehavior.Restrict); +``` + +### 🔁 方向解释 +- `HasOne` → Product 有一个 Category +- `WithMany` → Category 有多个 Product +- `Restrict` → 有子记录时禁止删除父记录 + +--- + +## 🧭 关系查询(必须 `Include`) + +```csharp +// 多 → 一 +await _db.Products.Include(p => p.Category).FirstAsync(); + +// 一 → 多 +await _db.Categories.Include(c => c.Products).FirstAsync(); +``` + +> ⚠️ **不写 `Include` → 导航属性为 `null`** + +--- + +## ❌ 常见踩坑点 + +| 问题 | 后果 | +|------|------| +| 忘记 `Include` | 导航属性为 null | +| 导航属性不是 `public` | EF Core 不识别 | +| 外键名不精确(如 `Categoryid` vs `CategoryId`) | 关系不成立 | +| `HasOne/WithMany` 配反 | 运行时关联错乱 | + +--- + +## ✅ 验证关系是否生效 + +1. **看 SQLite Viewer**:`Products` 表必须有 `CategoryId` 列 +2. **看迁移文件**:`Migrations/xxx_Init.cs` 中应有 + ```csharp + table.ForeignKey( + name: "FK_Products_Categories_CategoryId", + column: x => x.CategoryId, + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + ``` + +> 🚫 **不要再执行** `dotnet ef migrations add AddRelationship` — 关系已在第 2 章存在,再生成会是空迁移。 + +--- + +## 📌 一句话总结 + +> **一对多关系 = 集合导航(一)+ 引用导航+外键(多),查询必带 `Include`,配置推荐 `OnModelCreating` 明确写法。** + + +# 作业 \ No newline at end of file -- Gitee