diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" index 32e444e332f427445b18f8b973a6b75b06e5f72c..513976e7fa6ec43b3a50d654d26452d0f72b672a 100644 --- "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" @@ -1,4 +1,4 @@ -1. 安装依赖包:`dotnet add package Microsoft.EntityFramewordCore.SqlServer` +1. 安装依赖包:`dotnet add package Microsoft.EntityFrameworkCore.SqlServer` 2. 定义数据库表模型 ```csharp @@ -53,5 +53,6 @@ dotnet ef migrations add Init * ActionResult类型 将一般数据类型和HTTP状态信息混合使用 * 特定于格式的操作结果:如JsonResult和ContentResult * POCO(普通旧CLR对象) -@endmindmap + @endmindmap +`dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 9.0.0 --source "地址"` diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241216-Controller.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241216-Controller.md" new file mode 100644 index 0000000000000000000000000000000000000000..a53782ce7267dea8f149c1cfa9acc35240815fc5 --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241216-Controller.md" @@ -0,0 +1,104 @@ +```js +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using ScoreManager.Models; + +namespace ScoreManager.Controllers; +//学生 +public class StudentsController : Controller +{ + private readonly StudentDbContext _db; + + public StudentsController() + { + _db = new StudentDbContext(); + } + /// + /// 搜索 + /// + /// + [HttpGet] + public IActionResult Index() + { + var list = _db.Students.ToList(); + return View(list); + } + [HttpPost] + public IActionResult Index(string keyword) + { + keyword = string.IsNullOrEmpty(keyword) ? "" : keyword.Trim(); + if(string.IsNullOrEmpty(keyword)) + { + return View(_db.Students); + } + var list = _db.Students.Where(x => x.StudentName.Contains(keyword)).ToList(); + return View(list); + } + /// + /// 新增方法 + /// + /// + [HttpGet] + public IActionResult Create() + { + return View(); + } + [HttpPost] + public IActionResult Create(Student input) + { + //检查 _db.Students 中是否存在至少一个 StudentId 等于 input.StudentId 的学生记录。 + //如果存在(即 Any 方法返回 true),则返回 Create 视图。 + if (_db.Students.Any(x => x.StudentId.Equals(input.StudentId))) + { + return View("Create"); + } + _db.Students.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + /// + /// 删除 + /// + /// + public IActionResult Delete(int id) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == id); + return View(stu); + } + public IActionResult DeleteConfirm(int id) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == id); + if(stu != null) + { + _db.Students.Remove(stu); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + return NotFound(); + } + /// + /// 修改 + /// + /// + [HttpGet] + public IActionResult Edit(int id) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == id); + return View(stu); + } + [HttpPost] + public IActionResult Edit(Student input) + { + var stu = _db.Students.FirstOrDefault(x => x.Id == input.Id); + if(stu != null) + { + stu.StudentId = input.StudentId; + stu.StudentName = input.StudentName; + stu.Age = input.Age; + _db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(stu); + } +} +``` \ No newline at end of file diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241218-Model.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241218-Model.md" new file mode 100644 index 0000000000000000000000000000000000000000..6d66eebe2811378ffe0e4064dc87d5a60f56d654 --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241218-Model.md" @@ -0,0 +1,36 @@ +```js +//DbContext +using Microsoft.EntityFrameworkCore; + +namespace ScoreManager.Models; + +public class StudentDbContext : DbContext +{ + public DbSet Students {get;set;} = null!; + + public DbSet Courses {get;set;} = null!; + + public DbSet Scores {get;set;} = null!; + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + + var onString = $"server=.\\SQLEXPRESS;database=Db2;uid=sa;pwd=123456;TrustServerCertificate=true;"; + + optionsBuilder.UseSqlServer(onString); + } +} +``` +```js +//Student +namespace ScoreManager.Models; + +public class Student +{ + public int Id {get;set;} + public int Age {get;set;} + public int StudentId {get;set;} + public string StudentName {get;set;} = null!; +} +``` \ No newline at end of file diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241219-Views.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241219-Views.md" new file mode 100644 index 0000000000000000000000000000000000000000..10f88623a13da920902361d9f187f6e1ccd01565 --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241219-Views.md" @@ -0,0 +1,131 @@ +```html + + +@model List + + + +
+
+ + + 新增 +
+ + + + + + + + @foreach (var item in Model) + { + + + + + + + } +
Id年龄姓名操作
@item.Id@item.Age@item.StudentName + 编辑 + 删除 +
+``` + +```html + +@model ScoreManager.Models.Student; + + + +@using(Html.BeginForm("Edit","Students",FormMethod.Post)) +{ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + 取消 +
+} +``` + +```html + +@model ScoreManager.Models.Student + + + +@using(Html.BeginForm("DeleteConfirm","Students",FormMethod.Post)) +{ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + 取消 +
+} +``` + +```html + +@model ScoreManager.Models.Student + + +@using(Html.BeginForm("Create","Students",FormMethod.Post)) +{ +
+ + +
+
+ + +
+
+ + +
+
+ + 取消 +
+} +``` \ No newline at end of file