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 0000000000000000000000000000000000000000..e6bbca192082d41269b085f1469a184c6a1422d7 --- /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 0000000000000000000000000000000000000000..e5c0cb65521c34fb4aea8e2a8661573cc573e32c --- /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 0000000000000000000000000000000000000000..ab0cfa86999d0cec133c0d17c8e299349aa374ec --- /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 0000000000000000000000000000000000000000..4ccb2b866e356ec2688c7cdee7240f416468bfd7 --- /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 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 0000000000000000000000000000000000000000..622fbbe54cefaaf9cc68c9ed74f1075357692dc7 --- /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; } + } + ``` + +