From 365a904a10d762d1e30fd4fd47e7b1c552c4cf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E6=BB=A2=E9=92=B0?= <14091774+well-yes@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 20:56:04 +0800 Subject: [PATCH] 1 --- .../20241216.md" | 9 +++ .../20241218.md" | 9 +++ .../20241219.md" | 73 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 "\351\231\206\346\273\242\351\222\260/20241216.md" create mode 100644 "\351\231\206\346\273\242\351\222\260/20241218.md" create mode 100644 "\351\231\206\346\273\242\351\222\260/20241219.md" diff --git "a/\351\231\206\346\273\242\351\222\260/20241216.md" "b/\351\231\206\346\273\242\351\222\260/20241216.md" new file mode 100644 index 0000000..dce84f1 --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241216.md" @@ -0,0 +1,9 @@ +1. Select:选择或投影元素。 +2. Where:过滤元素。 +3. OrderBy / OrderByDescending:对结果进行排序。 +4. GroupBy:对元素进行分组。 +5. Join:执行联合操作,类似于SQL中的JOIN。 +6. Aggregate:执行聚合操作,如求和、平均值等。 +7. Any / All:检查集合中是否存在/全部元素满足条件。 +8. Count:返回集合中的元素数量。 +9. FirstOrDefault / LastOrDefault:返回集合中的第一个/最后一个元素,如果集合为空,则返回默认值。 \ No newline at end of file diff --git "a/\351\231\206\346\273\242\351\222\260/20241218.md" "b/\351\231\206\346\273\242\351\222\260/20241218.md" new file mode 100644 index 0000000..c7b163a --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241218.md" @@ -0,0 +1,9 @@ +## 1. 生成迁移文件 dotnet ef migrations add XXX + +1. 前提: + - 得安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.Design + - 需要一个工具,ef工具,安装命令:dotnet tool install --global dotnet-ef + - 要求程序不能编译错误,如果想确认有没有编译错误,请使用命令:dotnet build + - 程序不能处于运行状态 + +## 2. 将迁移文件同步到数据库 \ No newline at end of file diff --git "a/\351\231\206\346\273\242\351\222\260/20241219.md" "b/\351\231\206\346\273\242\351\222\260/20241219.md" new file mode 100644 index 0000000..86f4639 --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241219.md" @@ -0,0 +1,73 @@ +``` +using Microsoft.AspNetCore.Mvc; +using ScoreManager.Models; + +namespace ScoreManager.Controllers; + +public class CoursesController : Controller +{ + private readonly ScoreDbContext _db; + public CoursesController() + { + _db = new ScoreDbContext(); + } + public IActionResult Index(string keyword) + { + keyword = string.IsNullOrEmpty(keyword) ? "" : keyword.Trim(); + if (string.IsNullOrEmpty(keyword)) + { + var list = _db.Courses.ToList(); + return View(list); + } + var res = _db.Courses.Where(x => x.CourseName.Contains(keyword)).ToList(); + return View(res); + + } + public IActionResult Edit(int id) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == id); + return View(obj); + } + + [HttpPost] + public IActionResult Edit(Course input) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == input.Id); + if (obj == null) + { + return NotFound(); + } + obj.CourseName = input.CourseName; + _db.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Delete(int id) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == id); + return View(obj); + } + public IActionResult DeleteConfirm(int id) + { + var obj = _db.Courses.FirstOrDefault(x => x.Id == id); + if (obj == null) + { + return NotFound(); + } + _db.Courses.Remove(obj); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + public IActionResult Create() + { + return View(); + } + + [HttpPost] + public IActionResult Create(Course input) + { + _db.Courses.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } +} +``` \ No newline at end of file -- Gitee