From 7697a371139d3e413ac419dfdb1c69aac89b8bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=99=E7=82=9C=E5=BB=B7?= <1160093753@qq.com> Date: Sun, 22 Dec 2024 21:01:20 +0800 Subject: [PATCH] 35 --- .../20241216.md" | 57 +++++++ .../20241218.md" | 105 ++++++++++++ .../20241219.md" | 161 ++++++++++++++++++ 3 files changed, 323 insertions(+) create mode 100644 "\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241216.md" create mode 100644 "\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241218.md" create mode 100644 "\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241219.md" diff --git "a/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241216.md" "b/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241216.md" new file mode 100644 index 0000000..d173868 --- /dev/null +++ "b/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241216.md" @@ -0,0 +1,57 @@ +## 1.安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +## 2.定义数据库表模型 +- 在Models中的Blogs.cs中 +```c# +namespace Blog.Models; + +public class Blogs +{ + public int Id { get; set; } + public string Title { get; set; } = null!; + public string Content { get; set; } = null!; + public string Author { get; set; } = null!; +} +``` +## 3. 定义数据库上下文 +- 在Models中创建 BlogDbContext.cs文件 +```c# +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; } = null!; + + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=SK-20240829DIVS\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +``` + +## 4. 生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +``` +dotnet tool install -- global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef migration add InitialCreate +``` + +- 到这里要先在文件Blog.csproj中出现 +```c# + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + +``` +## 5.将上一步生成的迁移文件,更新到数据库:dotnet ef database update +`dotnet ef database update` +(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) + +- 结束后,在文件夹中会出现Migrations文件,就可以在数据库中看到你保存的数据 \ No newline at end of file diff --git "a/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241218.md" "b/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241218.md" new file mode 100644 index 0000000..c883ac4 --- /dev/null +++ "b/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241218.md" @@ -0,0 +1,105 @@ +1. 创建一个mvc项目 命令 `dotnet new mvc -o StudentManager` + +2. 模型 Student.cs + ```c# + namespace StudentManager.Models; + public class Student + { + public int Id{get;set;} //主键Id + public string StudentCode{get;set;}=null!; //学号 + public string StudentName{get;set;}=null!; //姓名 + } + ``` + +3. 数据库上下文 StudentDbContext.cs + ```c# + using Microsoft.EntityFrameworkCore; + namespace StudentManager.Models; + public class StudentDbContext:DbContext + { + public DbSetStudents{get;set;}=null!; + + //连接数据库 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuidler) + { + var str=$"server=SK-20240829LVRN\\SQLEXPRESS;database=StudentDb;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuidler.UseSqlServer(str); + } + } + + + //DbContext:安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.SqlServer + //1. 生成迁移文件 命令 dotnet ef migrations add XXX + 1.1 需要一个工具 ef工具 安装命令 dotnet tool install --global dotnet-ef + 1.2 安装依赖包 命令 dotnet add package Microsoft.EntityFrameworkCore.Design + 1.3 需要程序不能有编译错误,使用 dotnet build 查看是否有编译错误 + 1.4 程序不能处于运行状态 + //2. 将生成的迁移文件更新到数据库中 命令 dotnet ef database update + ``` + +4. 控制器 StudentsController.cs + ```c# + using Microsoft.AspNetCore.Mvc; + using StudentManager.Models; + namespace StudentManager.Controllers; + public class StudentsController:Controller + { + //引用数据库 + private readonly StudentDbContext _db; + public StudentsController() + { + _db=new StudentDbContext(); + } + + public IActionResult Index() + { + return View(); + } + } + ``` +5. 创建对应的视图 Students--Index.cshtml + ```c# + @model List; + +
+
+ +
+ 新增 +
+
+
+ + + + + + + + + @foreach(var item in @Model) + { + + + + + + + + } +
Id姓名性别年龄操作
@item.Id@item.Name@item.Sex@item.Age +
+ 编辑 +
+
+ 删除 +
+
+
+
+ ``` \ No newline at end of file diff --git "a/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241219.md" "b/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241219.md" new file mode 100644 index 0000000..cd6a9f7 --- /dev/null +++ "b/\351\230\231\347\202\234\345\273\267/\350\257\276\345\240\202\347\254\224\350\256\260/20241219.md" @@ -0,0 +1,161 @@ +### 新增功能 +1. Create.cshtml + ```c# + @model Student; +
+
+
+
+ +
+ ``` +2. 控制器 + ```c# + [HttpPost] + public IActionResult Create(Student input) + { + //将输入的内容添加的students表 + _db.Students.Add(input); + //保存到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + ``` + +### 修改功能 +1. Edit.cshtml + ```c# + @model Student; +
+
+
+
+ +
+ ``` +2. 控制器 + ```c# + /// + /// 获取修改内容 + /// + /// + /// + public IActionResult Edit(int id) + { + //通过点击传入的ID获取要修改此条数据的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 修改页面 + /// + /// + /// + [HttpPost] + public IActionResult Edit(Student input) + { + var list =_db.Students.FirstOrDefault(x=>x.Id==input.Id); + //判断list是否存在 + if(list!=null) + { + //修改的内容要在list中重新赋值 + list.Name=input.Name; + list.Sex=input.Sex; + list.Age=input.Age; + //重新传到数据库 + _db.SaveChanges(); + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` + +### 删除功能 +1. Delete.cshtml + ```c# + + + + + + + + + + + + + + + + + +
姓名:@Model.Name
性别:@Model.Sex
年龄:@Model.Age
+ 删除 + + 回到主页 +
+ ``` +2. 控制器 + ```c# + /// + /// 删除页面 + /// + /// + /// + public IActionResult Delete(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + return View(list); + } + /// + /// 确认删除页面 + /// + /// + /// + public IActionResult DeleteConfirm(int id) + { + //通过id获取删除的内容 + var list =_db.Students.FirstOrDefault(x=>x.Id==id); + //判断list是否为空 + //list不为空 + if(list!=null) + { + _db.Students.Remove(list); + //数据库的内容也需要删除 + _db.SaveChanges(); + //删除完重定向到index页面 + return RedirectToAction("Index"); + } + //不存在则提示错误 + return NotFound(); + } + ``` + +### 查询功能 +1. 在Index.cshtml中添加 + ```c# +
+ + +
+ ``` +2. 控制器 + ```c# + public IActionResult Index(string keyword) + { + //先判断keyword是否为空,如果不为空删除前后空格 + keyword=string.IsNullOrEmpty(keyword)?"":keyword.Trim(); + //keyword为空,返回在主页 + if(string.IsNullOrEmpty(keyword)) + { + var list=_db.Students.ToList(); + return View(list); + } + var res=_db.Students.Where(x=>x.Name.Contains(keyword)||x.Sex.Contains(keyword)).ToList(); + return View(res); + + } + ``` \ No newline at end of file -- Gitee