From 608fab0e2632b46fe218eeb394208e330d315bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A0=E9=9B=AA=E8=8A=B9?= <1508658027@qq.com> Date: Sun, 7 Jun 2026 23:39:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20260601-\345\256\211\350\243\205skd.md" | 13 ++++ ...33\345\273\272\351\241\271\347\233\256.md" | 20 +++++++ ...216 dotnet-ef \345\267\245\345\205\267.md" | 28 +++++++++ .../20260605-CRUR.md" | 59 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 "\345\215\240\351\233\252\350\212\271/20260601-\345\256\211\350\243\205skd.md" create mode 100644 "\345\215\240\351\233\252\350\212\271/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" create mode 100644 "\345\215\240\351\233\252\350\212\271/20260604-\350\243\205 NuGet \345\214\205\344\270\216 dotnet-ef \345\267\245\345\205\267.md" create mode 100644 "\345\215\240\351\233\252\350\212\271/20260605-CRUR.md" diff --git "a/\345\215\240\351\233\252\350\212\271/20260601-\345\256\211\350\243\205skd.md" "b/\345\215\240\351\233\252\350\212\271/20260601-\345\256\211\350\243\205skd.md" new file mode 100644 index 0000000..fcfcfc7 --- /dev/null +++ "b/\345\215\240\351\233\252\350\212\271/20260601-\345\256\211\350\243\205skd.md" @@ -0,0 +1,13 @@ +# 笔记 + +``` +安装SDK + +1.访问 https://dotnet.microsoft.com/download/dotnet/8.0 +2.下载 .NET 8.0 SDK(不要下错成 Runtime) +3.双击安装包,按提示完成 + + +验证安装 +终端输入:dotnet --version 输出形式 8.0.xxx +``` diff --git "a/\345\215\240\351\233\252\350\212\271/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" "b/\345\215\240\351\233\252\350\212\271/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" new file mode 100644 index 0000000..64a236f --- /dev/null +++ "b/\345\215\240\351\233\252\350\212\271/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" @@ -0,0 +1,20 @@ +# 笔记 + +``` + -创建 Web API 项目 + dotnet new webapi -n MyShopApi --use-controllers + 跑起来 + dotnet run + 浏览器访问 /swagger,看到默认接口 + -项目结构 +MyShopApi/ +├── Controllers/ +│ └── WeatherForecastController.cs # 默认示例 Controller,本章末尾删掉 +├── Properties/ +│ └── launchSettings.json # dotnet run 启动配置(端口、环境) +├── appsettings.json # 应用配置(生产/默认) +├── appsettings.Development.json # 开发环境配置(覆盖上面) +├── MyShopApi.csproj # 项目文件(依赖、目标框架) +├── Program.cs # 入口文件:服务注册、中间件管道 +└── obj/、bin/ +``` diff --git "a/\345\215\240\351\233\252\350\212\271/20260604-\350\243\205 NuGet \345\214\205\344\270\216 dotnet-ef \345\267\245\345\205\267.md" "b/\345\215\240\351\233\252\350\212\271/20260604-\350\243\205 NuGet \345\214\205\344\270\216 dotnet-ef \345\267\245\345\205\267.md" new file mode 100644 index 0000000..4b09376 --- /dev/null +++ "b/\345\215\240\351\233\252\350\212\271/20260604-\350\243\205 NuGet \345\214\205\344\270\216 dotnet-ef \345\267\245\345\205\267.md" @@ -0,0 +1,28 @@ +# 笔记 + +``` +安装NuGet 包 +dotnet add package Microsoft.EntityFrameworkCore.Sqlite +dotnet add package Microsoft.EntityFrameworkCore.Design + +安装dotnet-ef 工具 +dotnet tool install --global dotnet-ef +验证:dotnet ef --version + +在 appsettings.json 里加连接字符串: +``` + +{ +"Logging": { +"LogLevel": { +"Default": "Information", +"Microsoft.AspNetCore": "Warning" +} +}, +"AllowedHosts": "\*", +"ConnectionStrings": { +"Default": "Data Source=myshop.db" +} +} + +``` \ No newline at end of file diff --git "a/\345\215\240\351\233\252\350\212\271/20260605-CRUR.md" "b/\345\215\240\351\233\252\350\212\271/20260605-CRUR.md" new file mode 100644 index 0000000..3cadcbe --- /dev/null +++ "b/\345\215\240\351\233\252\350\212\271/20260605-CRUR.md" @@ -0,0 +1,59 @@ +# 笔记 +``` +[ApiController] +[Route("api/[controller]")] +public class ProductsController : ControllerBase +{ + private readonly AppDbContext _db; + + public ProductsController(AppDbContext db) => _db = db; + + [HttpGet] + public async Task>> GetAll() + { + var products = await _db.Products.ToListAsync(); + return Ok(products); + } + + [HttpGet("{id:int}")] + public async Task> GetById(int id) + { + var product = await _db.Products.FindAsync(id); + if (product is null) return NotFound(); + return Ok(product); + } + + [HttpPost] + public async Task> Create([FromBody] Product input) + { + _db.Products.Add(input); + await _db.SaveChangesAsync(); + return CreatedAtAction(nameof(GetById), new { id = input.Id }, input); + } + + [HttpPut("{id:int}")] + public async Task Update(int id, [FromBody] Product input) + { + var existing = await _db.Products.FindAsync(id); + if (existing is null) return NotFound(); + + existing.Name = input.Name; + existing.Price = input.Price; + existing.Stock = input.Stock; + existing.CategoryId = input.CategoryId; + + await _db.SaveChangesAsync(); + return NoContent(); + } + + [HttpDelete("{id:int}")] + public async Task Delete(int id) + { + var existing = await _db.Products.FindAsync(id); + if (existing is null) return NotFound(); + _db.Products.Remove(existing); + await _db.SaveChangesAsync(); + return NoContent(); + } +} +``` \ No newline at end of file -- Gitee From 397e25cb38f314a230ce25b2b008a36f43215713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A0=E9=9B=AA=E8=8A=B9?= <1508658027@qq.com> Date: Sun, 14 Jun 2026 19:03:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...74\350\210\252\345\261\236\346\200\247.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "\345\215\240\351\233\252\350\212\271/20260608-\344\270\200\345\257\271\345\244\232\345\257\274\350\210\252\345\261\236\346\200\247.md" diff --git "a/\345\215\240\351\233\252\350\212\271/20260608-\344\270\200\345\257\271\345\244\232\345\257\274\350\210\252\345\261\236\346\200\247.md" "b/\345\215\240\351\233\252\350\212\271/20260608-\344\270\200\345\257\271\345\244\232\345\257\274\350\210\252\345\261\236\346\200\247.md" new file mode 100644 index 0000000..c46383e --- /dev/null +++ "b/\345\215\240\351\233\252\350\212\271/20260608-\344\270\200\345\257\271\345\244\232\345\257\274\350\210\252\345\261\236\346\200\247.md" @@ -0,0 +1,23 @@ +# 笔记 +## 一、实体类配置 +1. 一的一方(Category) + - 包含集合导航属性:ICollection +2. 多的一方(Product) + - 包含外键:int CategoryId(命名规范:主表名 + Id) + - 包含引用导航属性:Category? Category +## 二、DbContext 关系配置 +``` +modelBuilder.Entity() + .HasOne(p => p.Category) // 商品属于一个分类 + .WithMany(c => c.Products) // 分类包含多个商品 + .HasForeignKey(p => p.CategoryId) // 外键字段 + .OnDelete(DeleteBehavior.Restrict); // 有子数据时禁止删除父数据 +``` +## 三、数据查询 +- 查商品 + 所属分类(多→一):`Include(p => p.Category)` +- 查分类 + 所有商品(一→多):`Include(c => c.Products)` +## 数据库中表关系的体现 +### 一种是数据库中实际存在关系 +- 数据库中直接创建外键关系(或者叫约束) +### 一种是数据中不实际存在关系 +- 不在数据库中直接创建外键关系,而在业务代码中,去强制维护这种关系 \ No newline at end of file -- Gitee