diff --git "a/\346\236\227\344\275\231\345\200\251/20241223\350\275\257\345\210\240\351\231\244.md" "b/\346\236\227\344\275\231\345\200\251/20241223\350\275\257\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..1ca2ab5f1e4aec04a1ad8d8d1bfd78aabc02b8ae --- /dev/null +++ "b/\346\236\227\344\275\231\345\200\251/20241223\350\275\257\345\210\240\351\231\244.md" @@ -0,0 +1,49 @@ +## 一.笔记 +### 1.软删除 +```js +软删除概念: + 软删除是一种逻辑删除方式,通过在数据库中标记记录为“已删除”,而不是物理地从数据库中删除记录。 + 这种方式保留了数据的历史信息,有助于维护数据完整性、简化数据恢复,并遵守相关法规要求。 + +软删除在MVC中的实现: + 在模型层,添加软删除标记字段(如IsDeleted),并在数据库表中相应添加该字段。 + 在控制器层,处理软删除请求,更新数据库中的软删除标记字段。 + 在视图层,根据软删除标记字段的值来过滤和显示数据 +``` +## 二.操作 +1. 数据库设计 +```js +public class Student{ + .... + // 是否删除,ture为已删除,false为未删除 + // 默认原始为未删除 + public bool IsDelete{get;set;}=false; + // 完成后迁移更新到数据库 +} +``` + +2. 控制器 +```js +// 首页 +public IActionResult Index() +{ + // 首页筛选条件 :选择没有删除的显示到页面中 则为false,不选择true已经删除的 + var list = _db.Student.Where(x=>x.IsDelete!=true); + return View(list); +} + + +// 删除 +public IActionResult DdeleteOther(int id) +{ + var cont = _db.Student.FirstOrDefault(x=>x.Id==id) + if(cont==null){ + return NotFound(); + } + // 学生表软删除 打开删除功能-更新表-保存到数据库 + _db.IsDelete=true; + _db.Student.Update(cont); + _db.SavaChanges(); + return RedirectToAction("Index") +} +``` \ No newline at end of file diff --git "a/\346\236\227\344\275\231\345\200\251/20241225\350\277\236\350\241\250\346\237\245\350\257\242.md" "b/\346\236\227\344\275\231\345\200\251/20241225\350\277\236\350\241\250\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..2ede4b01cd1fc68d3c6dda9ccba4225b161a244a --- /dev/null +++ "b/\346\236\227\344\275\231\345\200\251/20241225\350\277\236\350\241\250\346\237\245\350\257\242.md" @@ -0,0 +1,116 @@ +## 操作-连三张表 +```js +设计数据库: + +1. 成绩表 + /// + /// 主键Id + /// + public int Id { get; set; } + + /// + /// 学生Id,外键 + /// + public int StudentId { get; set; } + + /// + /// 课程Id,外键 + /// + public int CourseId { get; set; } + + /// + /// 学生的课程成绩 + /// + public decimal Scores { get; set; } + +2. 课程表 + /// + /// 主键Id + /// + public int Id { get; set; } + + /// + /// 课程名称 + /// + public string CourseName { get; set; } = null!; + +3. 学生表 + /// + /// 主键Id + /// + public int Id { get; set; } + + /// + /// 学生编号 + /// + public string StudentCode { get; set; } = null!; + + /// + /// 学生姓名 + /// + public string StudentName { get; set; } = null!; + + /// + /// 昵称 + /// + public string? Nickname { get; set; } + + /// + /// 是否删除,true为已经删除,false为没有删除 + /// + /// + public bool IsDeleted { get; set; } = false; + + public bool IsActived { get; set; } = true; + + public DateOnly Birthday { get; set; } + + public int Age { get; set; } + + public char Sex { get; set; } + + +``` + + + +```js +现在有三个表分别为成绩表,课程表,学生信息表,现在要连接三张表形成一张新的表,只显示三张表中的部分内容 +``` +```js + public IActionResult Index() + { + // 查出所有的成绩记录 + var scores = _db.Scores.ToList(); + // 查出所有的学生记录 + var students = _db.Students.Where(x => !x.IsDeleted).ToList(); + // 查出所有的课程记录 + var courses = _db.Courses.ToList(); + + // 遍历成绩记录 + var res = scores.Select(x => + { + // 找到和当前成绩记录中的学生Id匹配的学生信息 + var tmpStudent = students.FirstOrDefault(z => x.StudentId == z.Id); + // 找到和当前成绩记录中的课程Id匹配的课程信息 + var tmpCourse = courses.FirstOrDefault(z => x.CourseId == z.Id); + + // 分别处理学生、课程没有找到的情况 + var tmpStudentName = tmpStudent == null ? "" : tmpStudent.StudentName; + var tmpCourseName = tmpCourse == null ? "" : tmpCourse.CourseName; + + // 返回新的,包含学生姓名和课程名称的信息 + var res = new + { + x.Id, + x.StudentId, + StudentName = tmpStudentName, + x.CourseId, + CourseName = tmpCourseName, + x.Scores + }; + return res; + }); + return View(res); + } +``` \ No newline at end of file