From 9b5bd7308655724687c5b894b53b29ec367c1f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E7=9D=BF?= <3282028831@qq.com> Date: Sun, 22 Dec 2024 16:07:20 +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 --- ...6\345\272\223\350\277\236\346\216\245).md" | 131 +++++++++++++++ ...1\347\247\273\346\226\207\344\273\266).md" | 40 +++++ ...6\345\210\240\346\224\271\346\237\245).md" | 156 ++++++++++++++++++ 3 files changed, 327 insertions(+) create mode 100644 "\351\231\210\346\200\235\347\235\277/20241216-(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" create mode 100644 "\351\231\210\346\200\235\347\235\277/20241218-(\350\277\201\347\247\273\346\226\207\344\273\266).md" create mode 100644 "\351\231\210\346\200\235\347\235\277/20241220-(\345\242\236\345\210\240\346\224\271\346\237\245).md" diff --git "a/\351\231\210\346\200\235\347\235\277/20241216-(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" "b/\351\231\210\346\200\235\347\235\277/20241216-(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" new file mode 100644 index 0000000..4c63e9f --- /dev/null +++ "b/\351\231\210\346\200\235\347\235\277/20241216-(\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245).md" @@ -0,0 +1,131 @@ +# MVC 数据库连接步骤笔记 + +## 1. 项目初始化 + +创建一个新的 MVC 项目。 + +--- + +## 2. 添加必要的包和库 + +通过 **NuGet 包管理器** 或 **Package Manager Console** 安装 **Entity Framework (EF)**: + +```powershell +Install-Package EntityFramework +``` + +--- + +## 3. 配置数据库连接字符串 + +在 **Web.config** 文件中,添加或修改 `` 节点: + +```xml + + + +``` + +--- + +## 4. 创建数据模型(Model) + +在 **Models** 文件夹中,创建一个类(例如 `Product.cs`)定义表结构: + +```csharp +public class Product +{ + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + public string Description { get; set; } +} +``` + +--- + +## 5. 创建数据库上下文(DbContext) + +在 **Models** 文件夹中,创建一个上下文类(例如 `MyDbContext.cs`): + +```csharp +public class MyDbContext : DbContext +{ + public MyDbContext() : base("name=MyDbContext") { } + public DbSet Products { get; set; } +} +``` + +--- + +## 6. 配置数据库迁移 + +通过 **Package Manager Console** 启用和管理迁移: + +```powershell +Enable-Migrations +Add-Migration InitialCreate +Update-Database +``` + +--- + +## 7. 实现控制器(Controller) + +在 **Controllers** 文件夹中,创建控制器(例如 `ProductsController`)处理产品的增删改查逻辑: + +```csharp +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 等)类似 +} +``` + +--- + +## 8. 创建视图(View) + +在 **Views** 文件夹中,为控制器的操作方法创建对应的视图。例如,为 `Index` 方法创建产品列表视图: + +```html +@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\231\210\346\200\235\347\235\277/20241218-(\350\277\201\347\247\273\346\226\207\344\273\266).md" "b/\351\231\210\346\200\235\347\235\277/20241218-(\350\277\201\347\247\273\346\226\207\344\273\266).md" new file mode 100644 index 0000000..2413912 --- /dev/null +++ "b/\351\231\210\346\200\235\347\235\277/20241218-(\350\277\201\347\247\273\346\226\207\344\273\266).md" @@ -0,0 +1,40 @@ +### 生成迁移文件 + +#### 命令: +```bash +dotnet ef migrations add FirstInit +``` + +--- + +## 前提条件 + +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 +``` + + diff --git "a/\351\231\210\346\200\235\347\235\277/20241220-(\345\242\236\345\210\240\346\224\271\346\237\245).md" "b/\351\231\210\346\200\235\347\235\277/20241220-(\345\242\236\345\210\240\346\224\271\346\237\245).md" new file mode 100644 index 0000000..6d84b2b --- /dev/null +++ "b/\351\231\210\346\200\235\347\235\277/20241220-(\345\242\236\345\210\240\346\224\271\346\237\245).md" @@ -0,0 +1,156 @@ +# CRUD 操作实现 + +## 新增 + +### 视图代码 +使用表单提交新增数据: +```cs +@model Baby.Models.Babys; + +
+
+
+
+ +
+``` + +### 控制器代码 +在控制器中实现 `Create` 动作方法: +```cs +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); + + 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); + } +} +``` + +--- + +### 补充说明 + +1. **代码一致性** + - 所有动作方法都严格遵循 `HttpGet` 和 `HttpPost` 的分离原则。 + - 确保模型绑定和验证逻辑正确。 + +2. **防止安全漏洞** + - 对用户输入进行验证,如使用 `[ValidateAntiForgeryToken]` 防范 CSRF 攻击。 + - 确保输入参数的合法性,避免 SQL 注入。 + +3. **UI 优化建议** + - 可使用 `Bootstrap` 样式美化表单和表格,提升用户体验。 + - 在错误提示中使用 `@Html.ValidationSummary()` 或 `@Html.ValidationMessageFor()`。 + + + -- Gitee