From af57569c1eb1404f69f185997a94639dc527da46 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 22 Dec 2024 19:14:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5?= 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" | 40 ++++++++++ ...01\347\247\273\346\226\207\344\273\266.md" | 30 ++++++++ .../20241220CRUD\346\223\215\344\275\234.md" | 75 +++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 "\345\220\264\351\221\253\351\270\277/20241216\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\345\220\264\351\221\253\351\270\277/20241218\347\224\237\346\210\220\350\277\201\347\247\273\346\226\207\344\273\266.md" create mode 100644 "\345\220\264\351\221\253\351\270\277/20241220CRUD\346\223\215\344\275\234.md" diff --git "a/\345\220\264\351\221\253\351\270\277/20241216\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/\345\220\264\351\221\253\351\270\277/20241216\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..9fcf336 --- /dev/null +++ "b/\345\220\264\351\221\253\351\270\277/20241216\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,40 @@ +## 连接数据库 +### 连接实际数据库 +- 使用[无参数]的方案 +1. 在Program.cs文件中不需要填写任何的代码 +2. 在对应的Models文件中,需要有重写OnConfiguring的代码,继承base的代码则不再需要 +3. 将连接数据库的字符串代码写入重写OnConfiguring的代码中 +```cs +protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) +{ + base.OnConfiguring(optionsBuilder); + + var connectionString = $"Server=.;Database=MdBlog;uid=sa;pwd=123456;TrustServerCertificate=true"; + optionsBuilder.UseSqlServer(connectionString); +} +``` +### 连接数据库 +1. 在控制器代码中实例化数据库对象 +```cs +private readonly BlogDbContext _db; +public BlogsController() +{ + _db = new BlogDbContext(); +} +``` +2. 将原本的模拟数据库替换成实际数据库 +```cs +Db.Blogs --> _db.Blogs +``` + - 如果改完之后出现`An unhandled exception occurred while processing the request.`的报错,则在主页面表示显示的代码后加上**ToList()** + ```cs + return View(_db.Blogs.ToList()); + ``` +3. 在每次修改(增删改)操作结束之后,都要进行保存,将内容保存到数据库 +```cs +_db.SaveChanges(); +``` +## 打开sql快捷方式 +```cs +win+R services.msc +``` \ No newline at end of file diff --git "a/\345\220\264\351\221\253\351\270\277/20241218\347\224\237\346\210\220\350\277\201\347\247\273\346\226\207\344\273\266.md" "b/\345\220\264\351\221\253\351\270\277/20241218\347\224\237\346\210\220\350\277\201\347\247\273\346\226\207\344\273\266.md" new file mode 100644 index 0000000..50c775f --- /dev/null +++ "b/\345\220\264\351\221\253\351\270\277/20241218\347\224\237\346\210\220\350\277\201\347\247\273\346\226\207\344\273\266.md" @@ -0,0 +1,30 @@ +## 生成迁移文件 +- 命令:dotnet ef migrations add Init +### 前提条件 +1. **安装必要的依赖包:** + - **Design 依赖包** + 安装命令: + ```bash + dotnet add package Microsoft.EntityFrameworkCore.Design + ``` + - **全局工具包 (dotnet-ef)** + 安装命令: + ```bash + dotnet tool install --global dotnet-ef + ``` + +2. **程序状态要求:** + - 确保程序没有编译错误,可运行以下命令检查: + ```bash + dotnet build + ``` + - 确保程序未处于运行状态。 + +--- + +#### 同步迁移文件到数据库 + +#### 命令: +```bash +dotnet ef database update +``` \ No newline at end of file diff --git "a/\345\220\264\351\221\253\351\270\277/20241220CRUD\346\223\215\344\275\234.md" "b/\345\220\264\351\221\253\351\270\277/20241220CRUD\346\223\215\344\275\234.md" new file mode 100644 index 0000000..0d5e025 --- /dev/null +++ "b/\345\220\264\351\221\253\351\270\277/20241220CRUD\346\223\215\344\275\234.md" @@ -0,0 +1,75 @@ +# 新增 +```cs +@model Blog.Models.Blogs; +
+
+
+
+ +
+``` +```cs +public IActionResult Create() + { + + return View(); + } + [HttpPost] + public IActionResult Create(Blogs input) + { + var maxId=Db.Blogs.Select(t=>t.Id).Max(); + input.Id=maxId+1; + Db.Blogs.Add(input); + return RedirectToAction("Index"); + } + +``` +# 编辑 +```cs +public IActionResult Edit(int id) +{ + // 获取id + var Blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + return View(Blog); +} +[HttpPost] +public IActionResult Edit(Blogs input) +{ + // 获取Id + var blog = Db.Blogs.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"); +} +``` \ No newline at end of file -- Gitee