From 9861ea9a60b173da7901eb66c9584e16b957e088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=B6=B5?= <3234558314@qq.com> Date: Sun, 29 Dec 2024 21:19:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\346\225\260\346\215\256\345\272\223.md" | 92 +++---------------- ...23\344\274\252\345\210\240\351\231\244.md" | 33 +++++++ ...25\350\277\236\350\241\250\346\237\245.md" | 40 ++++++++ 3 files changed, 84 insertions(+), 81 deletions(-) create mode 100644 "\351\231\210\346\242\246\346\266\265/20241223\344\274\252\345\210\240\351\231\244.md" create mode 100644 "\351\231\210\346\242\246\346\266\265/20241225\350\277\236\350\241\250\346\237\245.md" diff --git "a/\351\231\210\346\242\246\346\266\265/20241218\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/\351\231\210\346\242\246\346\266\265/20241218\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" index d10bc12..6320f32 100644 --- "a/\351\231\210\346\242\246\346\266\265/20241218\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" +++ "b/\351\231\210\346\242\246\346\266\265/20241218\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" @@ -8,87 +8,17 @@ ``` ### 必要条件(一些安装命令) -1. 下载安装包:dotnet add package Microsoft.EntityFrameworkCore.SqlServer -2. 下载安装包:dotnet add package Microsoft.EntityFrameworkCore.Design -3. 下载ef工具:dotnet tool install --global dotnet-ef -4. 生成迁移文件:dotnet ef migrations add Info -5. 将迁移文件更新到数据库:dotnet ef database update - -### 代码练习 -2. 在控制器中的代码(返回的是最简单的视图) - -```cs -using Microsoft.AspNetCore.Mvc; -using StuInfo.Models; - -namespace StuInfo.Controllers; - -public class StudentsController:Controller -{ - private readonly StuContext _db; - public StudentsController(){ - _db=new StuContext(); - } - // 查询 - public IActionResult Index(int id) - { - var res=_db.Students.FirstOrDefault(x=>x.Id ==id); - return View(res); - } - public IActionResult Create(int id) - { - var res=_db.Students.FirstOrDefault(x=>x.Id ==id); - return View(res); - } - public IActionResult Edit(int id) - { - var res=_db.Students.FirstOrDefault(x=>x.Id ==id); - return View(res); - } - public IActionResult Delete(int id) - { - var res=_db.Students.FirstOrDefault(x=>x.Id == id); - return View(res); - } -} -``` -3. 数据表中的数据 -```js -namespace StuInfo.Models; - -// 学生信息表 -public class Student -{ - // 主键id - public int Id{get;set;} - // 学号 - public string StuCore{get;set;}=null!; - // 学生姓名 - public string StuName{get;set;}=null!; -} -``` -4. 连接、创建数据库代码 -```cs -namespace StuInfo.Models; - -using Microsoft.EntityFrameworkCore; - -public class StuContext : DbContext -{ - public DbSet Students {get;set;}=null!; - public DbSet Teachers{get;set;}=null!; - - // 调用OnConfiguring方法 - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - base.OnConfiguring(optionsBuilder); - - var connectionString=$"server=.\\SQLEXPRESS;database=MyStuDb;uid=sa;pwd=123456;TrustServerCertificate=true;"; - - optionsBuilder.UseSqlServer(connectionString); - } -} -``` +- 生成迁移文件命令是:dotnet ef migrations add 文件名 +- 下载安装包:dotnet add package Microsoft.EntityFrameworkCore.SqlServer + * 前提条件: + 1. 需要一个Design的依赖包;安装命令是: + dotnet add package Microsoft.EntityFrameworkCore.Design + 2. 需要一个工具包;这个工具是dotnet-ef;全局安装命令是:dotnet tool install --global dotnet-ef + 3. 程序不能有编译错误;可以使用(dotnet build)命令来排查编译错误 + 4. 程序不能处于运行状态 +- 快捷键`Win+R`打开cmd输入命令`services.msc`选择`SQL Server(SQLEXPRESS)`点击运行数据库===>最后在同步迁移文件到数据库 +(将上一步生成的迁移文件同步到数据库;命令是:dotnet ef database update) +```html 5. 视图内的代码 ```html diff --git "a/\351\231\210\346\242\246\346\266\265/20241223\344\274\252\345\210\240\351\231\244.md" "b/\351\231\210\346\242\246\346\266\265/20241223\344\274\252\345\210\240\351\231\244.md" new file mode 100644 index 0000000..a29a311 --- /dev/null +++ "b/\351\231\210\346\242\246\346\266\265/20241223\344\274\252\345\210\240\351\231\244.md" @@ -0,0 +1,33 @@ +# 伪删除 +### 1. 伪删除的概念 +- 伪删除是一种数据管理技术,它将数据标记为"已删除"状态,而不是从数据库中物理删除。 +- 这种方法保留了数据的完整性,便于未来恢复或审计。 + +### 步骤 +1. 先在表中添加一条字段,并赋值 + + //表示是否被删除,默认值为false + public bool IsDeleted{get;set;}=false; + +2. 在控制器删除操作中,添加以下代码 + + //删除(伪删除) + public IActionResult Delete2(int id){ + var s=_db.Course.FirstOrDefault(x=>x.Id==id); + if(s==null){ + return NotFound(); + } + //需将remove改为update,如果删除了则修改IsDeleted的值 + s.IsDeleted=true; + _db.Course.Update(s); + _db.SaveChanges(); + return RedirectToAction("Index"); + } +3. 在控制器页面的action方法中,进行一下操作 + + //表示筛选没有被删除新的信息 + return View(_db.Course) ----> return View(_db.Course.Where(x=>x.IsDeleted!=true)); + +### 5. 数据库交互 +- 数据库查询时,使用 `isDeleted = false` 或 `isActive = true` 条件来排除已伪删除的数据。 +- 更新操作时,设置 `isDeleted = true` 而不是执行 `DELETE` 语句。 diff --git "a/\351\231\210\346\242\246\346\266\265/20241225\350\277\236\350\241\250\346\237\245.md" "b/\351\231\210\346\242\246\346\266\265/20241225\350\277\236\350\241\250\346\237\245.md" new file mode 100644 index 0000000..2fbd4b7 --- /dev/null +++ "b/\351\231\210\346\242\246\346\266\265/20241225\350\277\236\350\241\250\346\237\245.md" @@ -0,0 +1,40 @@ +## 连表展示 + +>三个表分别为成绩表,课程表,学生信息表,现在要连接三张表形成一张新的表,显示三张表中的部分内容 +```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 -- Gitee