From 7cc883b240e300e9317bdb7e37f020b73f2e3d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E8=8B=93=E9=9C=8F?= <15016798+fanglingfei@user.noreply.gitee.com> Date: Sun, 7 Jun 2026 20:01:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E7=AC=94=E8=AE=B0=E5=92=8C?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20260601-\345\256\211\350\243\205SDK.md" | 15 +++++ ...33\345\273\272\351\241\271\347\233\256.md" | 22 +++++++ ...216 dotnet-ef \345\267\245\345\205\267.md" | 32 ++++++++++ .../20260605-CRUR\346\224\271\351\200\240.md" | 61 +++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 "\346\226\271\350\213\223\351\234\217/20260601-\345\256\211\350\243\205SDK.md" create mode 100644 "\346\226\271\350\213\223\351\234\217/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" create mode 100644 "\346\226\271\350\213\223\351\234\217/20260604-\350\243\205 NuGet \345\214\205\344\270\216 dotnet-ef \345\267\245\345\205\267.md" create mode 100644 "\346\226\271\350\213\223\351\234\217/20260605-CRUR\346\224\271\351\200\240.md" diff --git "a/\346\226\271\350\213\223\351\234\217/20260601-\345\256\211\350\243\205SDK.md" "b/\346\226\271\350\213\223\351\234\217/20260601-\345\256\211\350\243\205SDK.md" new file mode 100644 index 0000000..d227f1d --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260601-\345\256\211\350\243\205SDK.md" @@ -0,0 +1,15 @@ +## 笔记 + +``` +安装SDK + +1.访问 https://dotnet.microsoft.com/download/dotnet/8.0 +2.下载 .NET 8.0 SDK(不要下错成 Runtime) +3.双击安装包,按提示完成 + + +验证安装 +终端输入:dotnet --version 输出形式 8.0.xxx +``` + +## 项目 \ No newline at end of file diff --git "a/\346\226\271\350\213\223\351\234\217/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" "b/\346\226\271\350\213\223\351\234\217/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" new file mode 100644 index 0000000..b500222 --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260603-\345\210\233\345\273\272\351\241\271\347\233\256.md" @@ -0,0 +1,22 @@ +## 笔记 + +``` + -创建 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/ +``` + +## 项目 \ No newline at end of file diff --git "a/\346\226\271\350\213\223\351\234\217/20260604-\350\243\205 NuGet \345\214\205\344\270\216 dotnet-ef \345\267\245\345\205\267.md" "b/\346\226\271\350\213\223\351\234\217/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..a851ccb --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260604-\350\243\205 NuGet \345\214\205\344\270\216 dotnet-ef \345\267\245\345\205\267.md" @@ -0,0 +1,32 @@ +## 笔记 + +``` +安装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/\346\226\271\350\213\223\351\234\217/20260605-CRUR\346\224\271\351\200\240.md" "b/\346\226\271\350\213\223\351\234\217/20260605-CRUR\346\224\271\351\200\240.md" new file mode 100644 index 0000000..b8e37ca --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260605-CRUR\346\224\271\351\200\240.md" @@ -0,0 +1,61 @@ +## 笔记 +``` +[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