From 609a407de0d182b499e02b7a27c9821321d4ba48 Mon Sep 17 00:00:00 2001 From: unicorn-png Date: Sun, 7 Jun 2026 19:30:23 +0800 Subject: [PATCH 1/2] zy --- .../20260601-sdk\345\256\211\350\243\205.md" | 5 ++ ...71\347\233\256\345\210\233\345\273\272.md" | 5 ++ ...et\345\214\205\345\256\211\350\243\205.md" | 9 ++ .../20260605-crud\346\224\271\351\200\240.md" | 84 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 "\345\220\264\346\242\223\345\220\233/20260601-sdk\345\256\211\350\243\205.md" create mode 100644 "\345\220\264\346\242\223\345\220\233/20260603-\351\241\271\347\233\256\345\210\233\345\273\272.md" create mode 100644 "\345\220\264\346\242\223\345\220\233/20260604-nuget\345\214\205\345\256\211\350\243\205.md" create mode 100644 "\345\220\264\346\242\223\345\220\233/20260605-crud\346\224\271\351\200\240.md" diff --git "a/\345\220\264\346\242\223\345\220\233/20260601-sdk\345\256\211\350\243\205.md" "b/\345\220\264\346\242\223\345\220\233/20260601-sdk\345\256\211\350\243\205.md" new file mode 100644 index 0000000..e6bbca1 --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260601-sdk\345\256\211\350\243\205.md" @@ -0,0 +1,5 @@ +## 安装dotnet sdk + +- 访问 https://dotnet.microsoft.com/download/dotnet/8.0进行安装 + +- 验证安装使用dotnet --version \ No newline at end of file diff --git "a/\345\220\264\346\242\223\345\220\233/20260603-\351\241\271\347\233\256\345\210\233\345\273\272.md" "b/\345\220\264\346\242\223\345\220\233/20260603-\351\241\271\347\233\256\345\210\233\345\273\272.md" new file mode 100644 index 0000000..e5c0cb6 --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260603-\351\241\271\347\233\256\345\210\233\345\273\272.md" @@ -0,0 +1,5 @@ +## 项目创建 + +- 使用dotnet new webapi -n MyShopApi --use-controllers创建webapi项目 + +- 创建完成输入dotnet run运行 \ No newline at end of file diff --git "a/\345\220\264\346\242\223\345\220\233/20260604-nuget\345\214\205\345\256\211\350\243\205.md" "b/\345\220\264\346\242\223\345\220\233/20260604-nuget\345\214\205\345\256\211\350\243\205.md" new file mode 100644 index 0000000..ab0cfa8 --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260604-nuget\345\214\205\345\256\211\350\243\205.md" @@ -0,0 +1,9 @@ +## 项目需要用到的一蓝宝安装 + +- dotnet add package Microsoft.EntityFrameworkCore.Sqlite +- dotnet add package Microsoft.EntityFrameworkCore.Design +- 在命令后加上-v 8 , 安装8.0的版本 + +- dotnet-ef工具安装 + +- dotnet tool install --global dotnet-ef \ No newline at end of file diff --git "a/\345\220\264\346\242\223\345\220\233/20260605-crud\346\224\271\351\200\240.md" "b/\345\220\264\346\242\223\345\220\233/20260605-crud\346\224\271\351\200\240.md" new file mode 100644 index 0000000..4ccb2b8 --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260605-crud\346\224\271\351\200\240.md" @@ -0,0 +1,84 @@ +## 增删改查连接数据库实现 + +``` C# +using Microsoft.AspNetCore.Mvc; +using dec.Models; +using dec.Data; +using Microsoft.EntityFrameworkCore; + + +namespace dec.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class CategoriesController : ControllerBase +{ + private readonly AppDbcontext _context; + + public CategoriesController(AppDbcontext context) + { + _context = context; + } + + [HttpGet] + public async Task>> GetCate() + { + return await _context.categories.ToListAsync(); + } + + [HttpGet("{id}")] + public async Task> GetId(int id) + { + var category = await _context.categories.FindAsync(id); + + if (category == null) + { + return NotFound(); + } + + return category; + } + + [HttpPost] + public async Task> PostCate(Category category) + { + _context.categories.Add(category); + + await _context.SaveChangesAsync(); + + return CreatedAtAction(nameof(GetId), new { id = category.Id }, category); + } + + [HttpPut("{id}")] + public async Task PutCate(int id, Category category) + { + if (id != category.Id) + { + return BadRequest(); + } + + _context.Entry(category).State = EntityState.Modified; + + await _context.SaveChangesAsync(); + + return NoContent(); + } + + [HttpDelete("{id}")] + public async Task DeleteCate(int id) + { + var category = await _context.categories.FindAsync(id); + + if (category == null) + { + return NotFound(); + } + + _context.categories.Remove(category); + await _context.SaveChangesAsync(); + + return NoContent(); + } +} + +``` \ No newline at end of file -- Gitee From a24fb0f260f1c74bf267ed00b855cd277008d3a3 Mon Sep 17 00:00:00 2001 From: unicorn-png Date: Sun, 14 Jun 2026 21:27:27 +0800 Subject: [PATCH 2/2] zy --- ...74\350\210\252\345\261\236\346\200\247.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "\345\220\264\346\242\223\345\220\233/20260608\347\254\224\350\256\260-\345\257\274\350\210\252\345\261\236\346\200\247.md" diff --git "a/\345\220\264\346\242\223\345\220\233/20260608\347\254\224\350\256\260-\345\257\274\350\210\252\345\261\236\346\200\247.md" "b/\345\220\264\346\242\223\345\220\233/20260608\347\254\224\350\256\260-\345\257\274\350\210\252\345\261\236\346\200\247.md" new file mode 100644 index 0000000..622fbbe --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260608\347\254\224\350\256\260-\345\257\274\350\210\252\345\261\236\346\200\247.md" @@ -0,0 +1,32 @@ +## 笔记 + +一、核心概念 +一对多:主实体(一) → 子实体(多),通过外键关联,导航属性用来在实体间互相查询。 +例:班级(一) ↔ 学生(多) + +二、实体定义(EF Core) + +- 1. 主实体(一) + + ```csharp + public class Class + { + public int Id { get; set; } + public string Name { get; set; } + public List Students { get; set; } + } + ``` + +- 2. 子实体(多) + + ```csharp + public class Student + { + public int Id { get; set; } + public string StuName { get; set; } + public int ClassId { get; set; } + public Class Class { get; set; } + } + ``` + + -- Gitee