diff --git "a/\344\276\257\345\207\244\351\230\263/20241216(mvc\351\241\271\347\233\256\345\210\233\345\273\272\350\277\207\347\250\213\357\274\214\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223).md" "b/\344\276\257\345\207\244\351\230\263/20241216(mvc\351\241\271\347\233\256\345\210\233\345\273\272\350\277\207\347\250\213\357\274\214\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223).md" new file mode 100644 index 0000000000000000000000000000000000000000..7f7ba22ac88d6a8e9e9368e0799488434f84f83c --- /dev/null +++ "b/\344\276\257\345\207\244\351\230\263/20241216(mvc\351\241\271\347\233\256\345\210\233\345\273\272\350\277\207\347\250\213\357\274\214\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223).md" @@ -0,0 +1,76 @@ +# MVC 数据库连接步骤笔记 +## 1. 项目初始化 + +首先,创建一个新的 MVC 项目。 +## 2. 添加必要的包和库 + +为了在 MVC 项目中连接数据库,通常需要使用 **Entity Framework (EF)**。可以通过 **NuGet 包管理器** 安装: + +1. 右键点击项目名称,选择 **管理 NuGet 程序包**。 +2. 在 **浏览** 选项卡中,搜索 **Entity Framework**。 +3. 选择 **Entity Framework** 包,点击 **安装**。 +4. 等待安装完成后,关闭包管理器窗口。 + +或者,使用 **Package Manager Console** 运行以下命令: + +```powershell +Install-Package EntityFramework +``` + +## 3. 配置数据库连接字符串 + +数据库连接字符串定义了应用程序如何连接到数据库。在 **Web.config** 文件中进行配置: + +1. 打开 **Web.config** 文件。 +2. 在 `` 节点下找到 `` 节点。如果不存在,可以手动添加。 +3. 添加连接字符串。例如,连接到 **SQL Server** 数据库: + +```xml + + + +``` + +## 4. 创建数据模型(Model) + +数据模型代表数据库中的表结构。在 **Models** 文件夹中创建一个新的模型类: + +1. 右键点击 **Models** 文件夹,选择 **添加** > **类**。 +2. 命名,例如 **Product.cs**,然后点击 **添加**。 +3. 定义模型属性。 + +## 5. 创建数据库上下文(DbContext) + +**DbContext** 是 EF 提供的核心类,用于与数据库进行交互。在 **Models** 文件夹中创建一个新的上下文类: + +1. 右键点击 **Models** 文件夹,选择 **添加** > **类**。 +2. 命名,例如 **MyDbContext.cs**,然后点击 **添加**。 +3. 定义上下文类: + +## 6. 配置数据库迁移 + +数据库迁移允许通过代码管理数据库架构的变化。以下是启用迁移的步骤: + +1. 打开 **Package Manager Console**。 +2. 选择 **默认项目** 为包含 **DbContext** 的项目。 +3. 运行以下命令以启用迁移: + +## 7. 实现控制器(Controller) + +控制器负责处理用户请求,并与模型和视图进行交互。以下是创建 **ProductsController** 控制器的步骤: + +1. 右键点击 **Controllers** 文件夹,选择 **添加** > **控制器**。 +2. 选择 **MVC 5 控制器 - 空**,点击 **添加**。 +3. 命名,例如 **ProductsController**,点击 **添加**。 +4. 在控制器中定义动作方法。 + +## 8. 创建视图(View) + +视图负责呈现数据。以下是创建 **Index** 视图的步骤: + +1. 在 **Views** 文件夹下找到 **Products** 文件夹。如果没有,手动创建。 +2. 右键点击 **Products** 文件夹,选择 **添加** > **视图**。 +3. 选择 **List** 模板,点击 **添加**。 +4. 在 **Index.cshtml** 中,编辑视图以显示产品列表: \ No newline at end of file diff --git "a/\344\276\257\345\207\244\351\230\263/20241218(MVC\350\277\201\347\247\273\346\226\207\344\273\266).md" "b/\344\276\257\345\207\244\351\230\263/20241218(MVC\350\277\201\347\247\273\346\226\207\344\273\266).md" new file mode 100644 index 0000000000000000000000000000000000000000..50c775f5f393f90516adf34530a7665966de060e --- /dev/null +++ "b/\344\276\257\345\207\244\351\230\263/20241218(MVC\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/\344\276\257\345\207\244\351\230\263/20241220(MVC\351\241\271\347\233\256\345\242\236\357\274\214\345\210\240\351\231\244\357\274\214\347\274\226\350\276\221\357\274\214\346\237\245\350\257\242).md" "b/\344\276\257\345\207\244\351\230\263/20241220(MVC\351\241\271\347\233\256\345\242\236\357\274\214\345\210\240\351\231\244\357\274\214\347\274\226\350\276\221\357\274\214\346\237\245\350\257\242).md" new file mode 100644 index 0000000000000000000000000000000000000000..dbf7aff622b93409c2286386e99b8dc2680f1ed4 --- /dev/null +++ "b/\344\276\257\345\207\244\351\230\263/20241220(MVC\351\241\271\347\233\256\345\242\236\357\274\214\345\210\240\351\231\244\357\274\214\347\274\226\350\276\221\357\274\214\346\237\245\350\257\242).md" @@ -0,0 +1,155 @@ +# 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()`。 + +