diff --git "a/\345\206\234\344\273\225\351\271\217/2024.12.16-\346\225\260\346\215\256\345\272\223\347\254\224\350\256\260.md" "b/\345\206\234\344\273\225\351\271\217/2024.12.16-\346\225\260\346\215\256\345\272\223\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..797be66f7c10646e2c6fb8ea6a320d8a5f39dd0f --- /dev/null +++ "b/\345\206\234\344\273\225\351\271\217/2024.12.16-\346\225\260\346\215\256\345\272\223\347\254\224\350\256\260.md" @@ -0,0 +1,74 @@ +## 连接实际数据库 +### 有参 +1. 在Program.cs文件中添加连接数据库字符串 [有参的数据库上下文初始化方案] + + //查看服务器名称按快捷键win+R,输入services.msc + var connectionString = $"Server=LAPTOP-HQBO17EG;Database=Md; uid=sa;pwd=123456;TrustServerCertificate=True;"; + builder.Services.AddDbContext(options => + { + options.UseSqlServer(connectionString); + }); + builder.Services.AddScoped(); +2. 在Models文件中,构造一个类型为[DbContextOptions]的有参函数 + + public BlogDbContext(DbContextOptions options) : base(options) + { + + } +### 无参 +1. 无需在Program.cs文件中填写任何的代码,但需重写OnConfiguring方法 [无参的数据库上下文初始化方案] + + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + //添加连接数据库的字符串 + var connectionString= $"Server=.\\SQLEXPRESS;Database=Md; uid=sa;pwd=123456;TrustServerCertificate=True;"; + optionsBuilder.UseSqlServer(connectionString); + } +2. 在控制器代码中实例化数据库对象 + + // 私有只读字段 + private readonly BlogDbContext _db; + //构造函数 + public BlogsController() + { + _db = new BlogDbContext(); + } +3. 将原本的模拟数据库替换成实际数据库 + + Two.Student--> _db.Student + +4. 修改好的代码需保存所以需加上_db.SaveChanges(); + + _db.SaveChanges(); + +### 连接数据库 +1. 在控制器代码中实例化数据库对象 +```cs +private readonly BlogDbContext _db; +public BlogsController() +{ + _db = new BlogDbContext(); +} +``` +2. 将原本的模拟数据库替换成实际数据库 +``` +Db.Blogs --> _db.Blogs +``` + - 如果改完之后出现`An unhandled exception occurred while processing the request.`的报错,则在主页面表示显示的代码后加上**ToList()** + ```cs + return View(_db.Blogs.ToList()); + ``` +3. 在每次修改(增删改)操作结束之后,都要进行保存,将内容保存到数据库 +```cs +_db.SaveChanges(); +``` + +## 打开sql快捷方式 +win+R services.msc + +## join方法 + 用于将一个数组或集合中的元素连接成一个单一的字符串 + +语法:string.Join("连接符", 字符串数组/集合/对象数组) diff --git "a/\345\206\234\344\273\225\351\271\217/2024.12.18-Models+sql\346\223\215\344\275\234\347\254\224\350\256\260.md" "b/\345\206\234\344\273\225\351\271\217/2024.12.18-Models+sql\346\223\215\344\275\234\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..50c775f5f393f90516adf34530a7665966de060e --- /dev/null +++ "b/\345\206\234\344\273\225\351\271\217/2024.12.18-Models+sql\346\223\215\344\275\234\347\254\224\350\256\260.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\206\234\344\273\225\351\271\217/2024.12.22-\345\242\236\345\210\240\346\224\271\347\254\224\350\256\260.md" "b/\345\206\234\344\273\225\351\271\217/2024.12.22-\345\242\236\345\210\240\346\224\271\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..0d5e025ba93049734fa327138e21b069d6c3ab90 --- /dev/null +++ "b/\345\206\234\344\273\225\351\271\217/2024.12.22-\345\242\236\345\210\240\346\224\271\347\254\224\350\256\260.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