From 0d49bae9c14a33f370b2045ef2dfb9f97a6f0e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:26:06 +0000 Subject: [PATCH 01/10] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E9=99=88?= =?UTF-8?q?=E4=BC=9F=E9=91=AB/1213=20=E4=B8=BA=20=E9=99=88=E4=BC=9F?= =?UTF-8?q?=E9=91=AB/1213.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1213" => "\351\231\210\344\274\237\351\221\253/1213.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\344\274\237\351\221\253/1213" => "\351\231\210\344\274\237\351\221\253/1213.md" (100%) diff --git "a/\351\231\210\344\274\237\351\221\253/1213" "b/\351\231\210\344\274\237\351\221\253/1213.md" similarity index 100% rename from "\351\231\210\344\274\237\351\221\253/1213" rename to "\351\231\210\344\274\237\351\221\253/1213.md" -- Gitee From 2007928ea9e55f8b7d993611493a306d42240935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:26:18 +0000 Subject: [PATCH 02/10] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E9=99=88?= =?UTF-8?q?=E4=BC=9F=E9=91=AB/1211=20=E4=B8=BA=20=E9=99=88=E4=BC=9F?= =?UTF-8?q?=E9=91=AB/1211.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1211" => "\351\231\210\344\274\237\351\221\253/1211.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\344\274\237\351\221\253/1211" => "\351\231\210\344\274\237\351\221\253/1211.md" (100%) diff --git "a/\351\231\210\344\274\237\351\221\253/1211" "b/\351\231\210\344\274\237\351\221\253/1211.md" similarity index 100% rename from "\351\231\210\344\274\237\351\221\253/1211" rename to "\351\231\210\344\274\237\351\221\253/1211.md" -- Gitee From d4834afdff6881ed7b3175267ca5c9ce688e9231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:26:26 +0000 Subject: [PATCH 03/10] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E9=99=88?= =?UTF-8?q?=E4=BC=9F=E9=91=AB/1209=20=E4=B8=BA=20=E9=99=88=E4=BC=9F?= =?UTF-8?q?=E9=91=AB/1209.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1209" => "\351\231\210\344\274\237\351\221\253/1209.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\344\274\237\351\221\253/1209" => "\351\231\210\344\274\237\351\221\253/1209.md" (100%) diff --git "a/\351\231\210\344\274\237\351\221\253/1209" "b/\351\231\210\344\274\237\351\221\253/1209.md" similarity index 100% rename from "\351\231\210\344\274\237\351\221\253/1209" rename to "\351\231\210\344\274\237\351\221\253/1209.md" -- Gitee From 09eb5aba961422f85b5a620fa106c72338dc1b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:26:51 +0000 Subject: [PATCH 04/10] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E9=99=88?= =?UTF-8?q?=E4=BC=9F=E9=91=AB/1204=20=E4=B8=BA=20=E9=99=88=E4=BC=9F?= =?UTF-8?q?=E9=91=AB/20241204.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1204" => "\351\231\210\344\274\237\351\221\253/20241204.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\344\274\237\351\221\253/1204" => "\351\231\210\344\274\237\351\221\253/20241204.md" (100%) diff --git "a/\351\231\210\344\274\237\351\221\253/1204" "b/\351\231\210\344\274\237\351\221\253/20241204.md" similarity index 100% rename from "\351\231\210\344\274\237\351\221\253/1204" rename to "\351\231\210\344\274\237\351\221\253/20241204.md" -- Gitee From 9f52852256a7dc24c095374ae4fee005ed5f0612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:27:06 +0000 Subject: [PATCH 05/10] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E9=99=88?= =?UTF-8?q?=E4=BC=9F=E9=91=AB/1211.md=20=E4=B8=BA=20=E9=99=88=E4=BC=9F?= =?UTF-8?q?=E9=91=AB/20241211.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241211.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\344\274\237\351\221\253/1211.md" => "\351\231\210\344\274\237\351\221\253/20241211.md" (100%) diff --git "a/\351\231\210\344\274\237\351\221\253/1211.md" "b/\351\231\210\344\274\237\351\221\253/20241211.md" similarity index 100% rename from "\351\231\210\344\274\237\351\221\253/1211.md" rename to "\351\231\210\344\274\237\351\221\253/20241211.md" -- Gitee From d8a43a8d3fdaa50d3f8582d4967be73d1628ad60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:27:15 +0000 Subject: [PATCH 06/10] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E9=99=88?= =?UTF-8?q?=E4=BC=9F=E9=91=AB/1213.md=20=E4=B8=BA=20=E9=99=88=E4=BC=9F?= =?UTF-8?q?=E9=91=AB/20241213.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241213.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\344\274\237\351\221\253/1213.md" => "\351\231\210\344\274\237\351\221\253/20241213.md" (100%) diff --git "a/\351\231\210\344\274\237\351\221\253/1213.md" "b/\351\231\210\344\274\237\351\221\253/20241213.md" similarity index 100% rename from "\351\231\210\344\274\237\351\221\253/1213.md" rename to "\351\231\210\344\274\237\351\221\253/20241213.md" -- Gitee From 9f309f461c858e5b52e8f1c97af6d97762bbe41a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:27:26 +0000 Subject: [PATCH 07/10] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E9=99=88?= =?UTF-8?q?=E4=BC=9F=E9=91=AB/1209.md=20=E4=B8=BA=20=E9=99=88=E4=BC=9F?= =?UTF-8?q?=E9=91=AB/20241209.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241209.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\351\231\210\344\274\237\351\221\253/1209.md" => "\351\231\210\344\274\237\351\221\253/20241209.md" (100%) diff --git "a/\351\231\210\344\274\237\351\221\253/1209.md" "b/\351\231\210\344\274\237\351\221\253/20241209.md" similarity index 100% rename from "\351\231\210\344\274\237\351\221\253/1209.md" rename to "\351\231\210\344\274\237\351\221\253/20241209.md" -- Gitee From 7b19c6e21428c4e66aa65c416add981aa7d8f478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:27:50 +0000 Subject: [PATCH 08/10] 1216 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈伟鑫 <14089910+cwx050211@user.noreply.gitee.com> --- "\351\231\210\344\274\237\351\221\253/202241216.md" | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 "\351\231\210\344\274\237\351\221\253/202241216.md" diff --git "a/\351\231\210\344\274\237\351\221\253/202241216.md" "b/\351\231\210\344\274\237\351\221\253/202241216.md" new file mode 100644 index 0000000..dacf11d --- /dev/null +++ "b/\351\231\210\344\274\237\351\221\253/202241216.md" @@ -0,0 +1,11 @@ +# 实体模型生成数据库 +1. 安装数据库驱动包`dotnet add package Microsoft.EntityFrameworkCore.Sqlserver` +2. 创建数据库模型`xxxDbContext`所继承的类`DbContext` +3. 创建数据库上下文 +4. 配置数据库连接 +5. 执行数据库操作 +6. 数据迁移 + - 需要一个Design的依赖包;安装命令是:`dotnet add package Microsoft.EntityFrameworkCore.Design` + - 需要一个工具包;这个工具是dotnet-ef;全局安装命令是:`dotnet tool install --global dotnet-ef` + - 数据迁移命令:`dotnet ef migrations add InitCreate` + - 同步迁移文件更新到数据库:`dotnet ef database update \ No newline at end of file -- Gitee From 013fcb2243d7fa55f406ba3e262706e4803e142c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:28:55 +0000 Subject: [PATCH 09/10] 1218 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈伟鑫 <14089910+cwx050211@user.noreply.gitee.com> --- .../202241218.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "\351\231\210\344\274\237\351\221\253/202241218.md" diff --git "a/\351\231\210\344\274\237\351\221\253/202241218.md" "b/\351\231\210\344\274\237\351\221\253/202241218.md" new file mode 100644 index 0000000..7012223 --- /dev/null +++ "b/\351\231\210\344\274\237\351\221\253/202241218.md" @@ -0,0 +1,55 @@ +1. 搭建一个MVC项目(-o 命名)`dotnet new mvc -o StudentManger` +2. 在Properties里改端口`5000` +3. 在Views里的_ViewStart.cshtml改`Layout=null` +## 在Models里 +1. 按所需的数据表(Student.cs)定义类型 +```js +namespace StudentManger.Models; +public class Student +{ + public int Id { get; set; } + public string SName { get; set; } = null!; + public int Age { get; set; } + public decimal SHight { get; set; } + public string SHobby { get; set; } = null!; +} +``` +2. 在而建一个存储每个表的数据库(StudentDbContext.cs) +```js +using Microsoft.EntityFrameworkCore; +namespace StudentManger.Models; +public class StudentDbContext : DbContext +{ + public DbSet Students { get; set; } = null!; +} +``` +3. 在此之前得安装所继承的DbContext +```js +//如何安装??? + +- 在该项目之下;安装数据库驱动包;命令是: +dotnet add package Microsoft.EntityFrameworkCore.Sqlserver +``` +4. 在StudentDbContext.cs里写配置代码 +```js + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + //这里调用DbContext类型中的原始OnConfiguring方法:以初始化一些原始配置 + base.OnConfiguring(optionsBuilder); + var conString = $"server=.\\SQLEXPRESS;database=StudentDb;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(conString); + } +``` +5. 生成迁移文件 +```js +//如何数据迁移??? +-命令是:dotnet ef migrations add 文件名 + * 前提条件: + 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) +``` \ No newline at end of file -- Gitee From 96252b34a80230781fbb7a2e4eef218554fd027e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F=E9=91=AB?= <14089910+cwx050211@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 14:30:22 +0000 Subject: [PATCH 10/10] 1220 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈伟鑫 <14089910+cwx050211@user.noreply.gitee.com> --- .../202241220.md" | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 "\351\231\210\344\274\237\351\221\253/202241220.md" diff --git "a/\351\231\210\344\274\237\351\221\253/202241220.md" "b/\351\231\210\344\274\237\351\221\253/202241220.md" new file mode 100644 index 0000000..aadc73b --- /dev/null +++ "b/\351\231\210\344\274\237\351\221\253/202241220.md" @@ -0,0 +1,106 @@ +# 新增 +```cs +@model Baby.Models.Babys; +
+
+
+
+ +
+``` +```cs +public IActionResult Create() + { + + return View(); + } + [HttpPost] + public IActionResult Create(Babys input) + { + var maxId=Db.Babys.Select(t=>t.Id).Max(); + input.Id=maxId+1; + Db.Babys.Add(input); + return RedirectToAction("Index"); + } + +``` +# 编辑 +```cs +public IActionResult Edit(int id) +{ + // 获取id + var baby = Db.Babys.FirstOrDefault(x => x.Id == id); + return View(baby); +} +[HttpPost] +public IActionResult Edit(Babys input) +{ + // 获取Id + var blog = Db.Babys.FirstOrDefault(x => x.Id == input.Id); + + if (blog != null) + { + blog.Title = input.Title; + blog.Content = input.Content; + blog.Author = input.Author; + } + return RedirectToAction("Index"); +} + +``` +# 删除 +```cs +public ActionResult Delete(int id) +{ + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + return View(product); +} +[HttpPost, ActionName("Delete")] +[ValidateAntiForgeryToken] +public ActionResult DeleteConfirmed(int id) +{ + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + db.Products.Remove(product); + db.SaveChanges(); + return RedirectToAction("Index"); +} +``` +# 查 +```cs +using System.Linq; +using System.Web.Mvc; +using MvcDatabaseConnection.Models; + +public class ProductsController : Controller +{ + private MyDbContext db = new MyDbContext(); + + // 显示产品列表 + public ActionResult Index() + { + var products = db.Products.ToList(); + return View(products); + } + + // 显示单个产品详情 + public ActionResult Details(int id) + { + var product = db.Products.Find(id); + if (product == null) + { + return HttpNotFound(); + } + return View(product); + } + + // 其他动作方法(Create, Edit, Delete)... +} +``` \ No newline at end of file -- Gitee