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 0000000000000000000000000000000000000000..dacf11d7a00d25f3fb2722787ca72690feabec4a --- /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 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 0000000000000000000000000000000000000000..7012223047cc14a4b6b5889474d48b9d20462f60 --- /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 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 0000000000000000000000000000000000000000..aadc73b6d70d08ce6c0a171a4e4f2ec93c554719 --- /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 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" diff --git "a/\351\231\210\344\274\237\351\221\253/1209" "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" rename to "\351\231\210\344\274\237\351\221\253/20241209.md" diff --git "a/\351\231\210\344\274\237\351\221\253/1211" "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" rename to "\351\231\210\344\274\237\351\221\253/20241211.md" diff --git "a/\351\231\210\344\274\237\351\221\253/1213" "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" rename to "\351\231\210\344\274\237\351\221\253/20241213.md"