From 8734fa3433e522ca1a9ebaa56ab8953aa8a05899 Mon Sep 17 00:00:00 2001 From: queyongzhen <14089735+queyongzhen@user.noreply.gitee.com> Date: Sun, 22 Dec 2024 15:44:33 +0800 Subject: [PATCH] =?UTF-8?q?12.20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20241111(MVC).md" | 0 .../20241113(MVC\346\236\266\346\236\204).md" | 0 ...6\345\272\223\350\277\236\346\216\245).md" | 110 +++++++++++++++ ...1\347\247\273\346\226\207\344\273\266).md" | 32 +++++ ...6\345\210\240\346\224\271\346\237\245).md" | 129 ++++++++++++++++++ 5 files changed, 271 insertions(+) rename "\351\230\231\346\263\263\347\217\215/20241111(MVC)\347\254\224\350\256\260.md" => "\351\230\231\346\263\263\347\217\215/20241111(MVC).md" (100%) rename "\351\230\231\346\263\263\347\217\215/20241113(MVC\346\236\266\346\236\204)\347\254\224\350\256\260.md" => "\351\230\231\346\263\263\347\217\215/20241113(MVC\346\236\266\346\236\204).md" (100%) create mode 100644 "\351\230\231\346\263\263\347\217\215/20241216(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" create mode 100644 "\351\230\231\346\263\263\347\217\215/20241218(\350\277\201\347\247\273\346\226\207\344\273\266).md" create mode 100644 "\351\230\231\346\263\263\347\217\215/20241220(\345\242\236\345\210\240\346\224\271\346\237\245).md" diff --git "a/\351\230\231\346\263\263\347\217\215/20241111(MVC)\347\254\224\350\256\260.md" "b/\351\230\231\346\263\263\347\217\215/20241111(MVC).md" similarity index 100% rename from "\351\230\231\346\263\263\347\217\215/20241111(MVC)\347\254\224\350\256\260.md" rename to "\351\230\231\346\263\263\347\217\215/20241111(MVC).md" diff --git "a/\351\230\231\346\263\263\347\217\215/20241113(MVC\346\236\266\346\236\204)\347\254\224\350\256\260.md" "b/\351\230\231\346\263\263\347\217\215/20241113(MVC\346\236\266\346\236\204).md" similarity index 100% rename from "\351\230\231\346\263\263\347\217\215/20241113(MVC\346\236\266\346\236\204)\347\254\224\350\256\260.md" rename to "\351\230\231\346\263\263\347\217\215/20241113(MVC\346\236\266\346\236\204).md" diff --git "a/\351\230\231\346\263\263\347\217\215/20241216(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" "b/\351\230\231\346\263\263\347\217\215/20241216(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" new file mode 100644 index 0000000..1afee5a --- /dev/null +++ "b/\351\230\231\346\263\263\347\217\215/20241216(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" @@ -0,0 +1,110 @@ +# MVC 数据库连接步骤 + +## 项目初始化 +- 创建一个新的 MVC 项目。 + +## 添加必要的包和库 +- 通过 **NuGet 包管理器** 或 **Package Manager Console** 安装 **Entity Framework (EF)**: + ```powershell + Install-Package EntityFramework + ``` + + +## 配置数据库连接字符串 +- 在 Web.config 文件中,添加或修改 节点: +``` + + + +``` + + +## 创建数据模型(Model) +- 在 Models 文件夹中,创建一个类(例如 Product.cs)定义表结构: +``` +public class Product +{ + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + public string Description { get; set; } +} +``` + + +## 创建数据库上下文(DbContext) +- 在 Models 文件夹中,创建一个上下文类(例如 MyDbContext.cs): +``` +public class MyDbContext : DbContext +{ + public MyDbContext() : base("name=MyDbContext") { } + public DbSet Products { get; set; } +} +``` + + +## 配置数据库迁移 +- 通过 Package Manager Console 启用和管理迁移: +``` +Enable-Migrations +Add-Migration InitialCreate +Update-Database +``` + +## 实现控制器(Controller) +- 在 Controllers 文件夹中,创建控制器(例如 ProductsController)处理产品的增删改查逻辑: +``` +public class ProductsController : Controller +{ + private MyDbContext db = new MyDbContext(); + + public ActionResult Index() => View(db.Products.ToList()); + public ActionResult Create() => View(); + [HttpPost] + public ActionResult Create(Product product) + { + if (ModelState.IsValid) + { + db.Products.Add(product); + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(product); + } + // 其他方法(Edit、Delete 等)类似 +} +``` + + +## 创建视图(View) +- 在 Views 文件夹中,为控制器的操作方法创建对应的视图。例如,为 Index 方法创建产品列表视图: +``` +@model IEnumerable +

产品列表

+ + + + + + + + @foreach (var item in Model) + { + + + + + + + } +
名称价格描述操作
@item.Name@item.Price@item.Description + @Html.ActionLink("编辑", "Edit", new { id = item.Id }) | + @Html.ActionLink("删除", "Delete", new { id = item.Id }) +
+``` + + + + diff --git "a/\351\230\231\346\263\263\347\217\215/20241218(\350\277\201\347\247\273\346\226\207\344\273\266).md" "b/\351\230\231\346\263\263\347\217\215/20241218(\350\277\201\347\247\273\346\226\207\344\273\266).md" new file mode 100644 index 0000000..3abba7a --- /dev/null +++ "b/\351\230\231\346\263\263\347\217\215/20241218(\350\277\201\347\247\273\346\226\207\344\273\266).md" @@ -0,0 +1,32 @@ +# 生成迁移文件 +- 命令: +``` +dotnet ef migrations add FirstInit +``` + +- 前提条件:安装必要的依赖包:**Design 依赖包** +安装命令: +``` +dotnet add package Microsoft.EntityFrameworkCore.Design +``` + +- 全局工具包 (dotnet-ef) +安装命令: +``` +dotnet tool install --global dotnet-ef +``` + +- 程序状态要求: + - 确保程序没有编译错误,可运行以下命令检查: +``` +dotnet build +``` + +**确保程序未处于运行状态。** + + +## 同步迁移文件到数据库 +- 命令: +``` +dotnet ef database update +``` \ No newline at end of file diff --git "a/\351\230\231\346\263\263\347\217\215/20241220(\345\242\236\345\210\240\346\224\271\346\237\245).md" "b/\351\230\231\346\263\263\347\217\215/20241220(\345\242\236\345\210\240\346\224\271\346\237\245).md" new file mode 100644 index 0000000..98b59b1 --- /dev/null +++ "b/\351\230\231\346\263\263\347\217\215/20241220(\345\242\236\345\210\240\346\224\271\346\237\245).md" @@ -0,0 +1,129 @@ +# CRUD操作 +## 新增数据 +### 视图代码 +* 使用表单提交新增数据: +``` +@model Baby.Models.Babys; + +
+
+
+
+ +
+``` + +### 控制器代码 +* 在控制器中实现 `Create` 动作方法: +``` +public IActionResult Create() +{ + return View(); +} + +[HttpPost] +public IActionResult Create(Babys input) +{ + // 自动生成 Id 并新增数据 + var maxId = Db.Babys.Select(t => t.Id).Max(); + input.Id = maxId + 1; + Db.Babys.Add(input); + Db.SaveChanges(); // 保存更改 + return RedirectToAction("Index"); +} +``` + + +## 编辑数据 +### 控制器代码 +* 实现编辑功能,支持加载数据和保存修改: +``` +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 baby = Db.Babys.FirstOrDefault(x => x.Id == input.Id); + if (baby != null) + { + baby.Title = input.Title; + baby.Content = input.Content; + baby.Author = input.Author; + } + Db.SaveChanges(); // 保存更改 + return RedirectToAction("Index"); +} +``` + + + +## 删除数据 +### 控制器代码 +* 分两步实现删除操作:显示确认页面和处理删除逻辑。 +``` +public ActionResult Delete(int id) +{ + // 查找要删除的记录 + var baby = Db.Babys.Find(id); + if (baby == null) + { + return HttpNotFound(); + } + return View(baby); +} + +[HttpPost, ActionName("Delete")] +[ValidateAntiForgeryToken] +public ActionResult DeleteConfirmed(int id) +{ + // 确认删除并保存更改 + var baby = Db.Babys.Find(id); + if (baby == null) + { + return HttpNotFound(); + } + Db.Babys.Remove(baby); + Db.SaveChanges(); + return RedirectToAction("Index"); +} +``` + + + +## 查询数据 +### 控制器代码 +* 实现列表展示和单个详情查看: +``` +using System.Linq; +using System.Web.Mvc; +using MvcDatabaseConnection.Models; + +public class BabysController : Controller +{ + private MyDbContext db = new MyDbContext(); + + // 显示宝宝列表 + public ActionResult Index() + { + var babys = db.Babys.ToList(); + return View(babys); + } + + // 显示单个宝宝详情 + public ActionResult Details(int id) + { + var baby = db.Babys.Find(id); + if (baby == null) + { + return HttpNotFound(); + } + return View(baby); + } +} +``` \ No newline at end of file -- Gitee