diff --git "a/\346\236\227\344\275\231\345\200\251/20241216\344\275\277\347\224\250\346\227\240\345\217\202\350\277\236\346\216\245sql\345\256\236\347\216\260\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\346\236\227\344\275\231\345\200\251/20241216\344\275\277\347\224\250\346\227\240\345\217\202\350\277\236\346\216\245sql\345\256\236\347\216\260\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..8d7acd62605648f1be3c1908bce79e4274ce6371 --- /dev/null +++ "b/\346\236\227\344\275\231\345\200\251/20241216\344\275\277\347\224\250\346\227\240\345\217\202\350\277\236\346\216\245sql\345\256\236\347\216\260\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,241 @@ +## 一.笔记 +### 1. 无参连接数据库 +```js + 1. 连接数据库方式:实参连接数据库;无参连接数据库 + // 无参为例 + 2. 大概:编写实例对象,创建一个数据库映射(eg:BlogDbContext)后编写无参构造函数,数据迁移和更新 +``` +### 2. 方法过程中代码意思 +1. DbContext +```js +* DbContext是Entity Framework中的一个类,它充当了(数据访问层)和(业务逻辑层)之间的桥梁。 +* 在MVC框架中,DbContext用于管理应用程序与数据库之间的连接和交互,提供了一种简单且一致的方式来操作数据库。 +* DbContext 是 EF Core 的核心类,它表示与数据库会话的上下文,并管理数据库中的实体。 + + eg: + // Models中创建了一个BlogDbContext.cs文件 + using Microsoft.EntityFrameworkCore; + namespace One.Models; + + public class OneDbContext:DbContext + { + // Shuju是一个类,里面存储了一些属性 + public DbSet Shuju{get;set;}=null!; + + // 无参连接数据库 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + // .\\SQLEXPRESS 教师端 + var connectionString = $"Server=黑炭;Database=Blog1;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseAzureSql(connectionString); + } + } +``` +2. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) +```js +* protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 是一个在 Entity Framework Core (EF Core) 中定义在 DbContext 子类中的方法。 + +* OnConfiguring 方法是一个受保护的重写(override)方法,它在 DbContext 实例被创建并配置时被调用。这个方法的目的是允许开发者在 DbContext 被实例化时对其进行配置。 + +* DbContextOptionsBuilder 是一个用于构建 DbContextOptions 的构建器。DbContextOptions 包含了一系列配置,这些配置指导 DbContext 如何连接到数据库以及如何处理数据库连接的其他方面(如日志记录、事务等)。 + +* 在 OnConfiguring 方法内部,你可以使用 optionsBuilder 来配置 DbContext。例如,你可以设置数据库连接字符串、配置日志级别或启用敏感数据的日志记录等。 + + // 在这里配置数据库连接字符串 + optionsBuilder.UseSqlServer("YourConnectionStringHere"); + + eg: + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + // .\\SQLEXPRESS 教师端 + var connectionString = $"Server=黑炭;Database=Blog1;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(connectionString); + } +``` +3. private readonly OneDbContext _db; + public OnesController() + { + _db = new OneDbContext(); + } +```js +// 构造函数 + private readonly OneDbContext _db; + public OnesController() + { + _db = new OneDbContext(); + } + + private 访问修饰符-私有的 + + readonly 只读-一旦对象被创建,readonly 字段的值就不能再被修改了 + + OneDbContext 映射数据库的方法名 + + _db 变量名 + + public OnesController() + { + _db = new OneDbContext(); + } 在Ones控制器中实例化OneDbContext(),并且变量名为:_db +``` +4. SaveChanges方法 +```js +* SaveChanges 方法是一个非常重要的操作,它用于将当前上下文(context)中所做的更改持久化到数据库中。 +``` + + + + +## 二.作业-连接数据库实现增删改查 +### Controller : OnesController +```js + using Microsoft.AspNetCore.Mvc; +using One.Models; +namespace One.Controllers; + +// _db.Shuju.ToList() 后面的ToList()是必要的,在执行的过程中会报错,说模型中的数据类型和视图中的数据类型不匹配,因为视图中的数据类型是List<>,而模型中的数据类型是DbDet<>,因此在返回视图中的时候必须要转换数据类型 + +public class OnesController : Controller +{ + private readonly OneDbContext _db; + public OnesController() + { + _db = new OneDbContext(); + } + // 首页 + public IActionResult Index(string keyword) + { + if(string.IsNullOrEmpty(keyword)) + { + _db.SaveChanges(); + + return View(_db.Shuju.ToList()); + } + else + { + var list = _db.Shuju.Where(x=>x.Title.Contains(keyword) + ||x.Content.Contains(keyword) + ||x.Author.Contains(keyword)).ToList(); + _db.SaveChanges(); + return View(list.ToList()); + } + // return View(_db.Shuju.ToList()); + } + // 增加 + public IActionResult Adds() + { + + return View(); + } + [HttpPost] + public IActionResult Adds(Shuju shu) + { + // Shuju只是一个数据类型 shu为变量名 只是需要这个类型中的某个值 + // 获取用户输入的数据,添加到 + + // 获取id的最大值,让它+1 + // var maxId = _db.Shuju.Select(x=>x.Id).Max(); + // 用户输入input的id值为最大值+1 + // shu.Id = maxId+1; + // 添加到集合中 + _db.Shuju.Add(shu); + _db.SaveChanges(); + // 添加好之后返回到首页 + return RedirectToAction("Index"); + } + + + // 编辑 + public IActionResult Edit(int id) + { + // 根据id 传入显示相应的数据 FirstOrDefault + var cont = _db.Shuju.FirstOrDefault(x=>x.Id==id); + return View(cont); + } + [HttpPost] + public IActionResult Edit(Shuju input) + { + // 如果要找的id和用户要修改的id内容相同 + var cont = _db.Shuju.FirstOrDefault(x=>x.Id==input.Id); + // 且要修改的id中的内容不为null + if(cont!=null) + { + cont.Title=input.Title; + cont.Content=input.Content; + cont.Author=input.Author; + _db.SaveChanges(); + + }; + return RedirectToAction("Index"); + } + + // 删除 + public IActionResult Delete(int id) + { + var cont = _db.Shuju.FirstOrDefault(x=>x.Id==id); + if(cont!=null) + { + + _db.SaveChanges(); + return View(cont); + + } + return View(); + } + public IActionResult DeleteOther(int id) + { + var cont = _db.Shuju.FirstOrDefault(x=>x.Id==id); + if(cont!=null) + { + + _db.Shuju.Remove(cont); + _db.SaveChanges(); + + } + return RedirectToAction("Index"); + + } +} + +``` + +### Models +```js +1. OneDbContext.cs + +using Microsoft.EntityFrameworkCore; +namespace One.Models; + +public class OneDbContext:DbContext +{ + public DbSet Shuju{get;set;}=null!; + + + // 无参连接数据库 + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + // .\\SQLEXPRESS 教师端 + var connectionString = $"Server=黑炭;Database=Blog1;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseAzureSql(connectionString); + + } + +} +``` +```js +2. Shuju + + namespace One.Models; + + public class Shuju + { + public int Id{get;set;} + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; + } +``` \ No newline at end of file diff --git "a/\346\236\227\344\275\231\345\200\251/20241218Models+sql\346\223\215\344\275\234.md" "b/\346\236\227\344\275\231\345\200\251/20241218Models+sql\346\223\215\344\275\234.md" new file mode 100644 index 0000000000000000000000000000000000000000..0906d5f3e243d54ba923fbc0feb25ca015a4d9b2 --- /dev/null +++ "b/\346\236\227\344\275\231\345\200\251/20241218Models+sql\346\223\215\344\275\234.md" @@ -0,0 +1,120 @@ +## 一.笔记 +### Models +```js + 1. 定义一张装实例对象的表 如Book + public class Book{ + public int Id{get;set;} + public int BookName{get;set;}=null!; + } + + 2. 定义一张连接数据库的表 如BookDbContext + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ + base.OnConfiguring(optionsBuilder); + + var contionString=$"server=.;dabase=;uis=;pwd=;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(contionString); + } + /* + 1. 生成迁移文件 命令:dotnet ef migrations add 自己取名 + + 前提条件: + 1. 需要一个Design的一依赖包,安装命令:dotnet add backage Microsoft.EntityFrameworkCore.Design + 2. 需要一个工具包,安装命令:dotnet tool install --global dotnet-ef + 3. 程序不能有编译错误,可以使用dotnet build命令来排查编译错误 + 4. 程序不能处于运行状态 + + 2. 将上一步生成的迁移文件同步到数据库,命令:dotnet ef database update + */ + +``` +### Controller +```js +// 注意引用EF Core包 + privite readonly 数据库表名 _db; + public 控制器名(){ + _db = new 数据库表名(); + } + +``` + + +## 二.练习 +### Controller +```js +using Microsoft.AspNetCore.Mvc; +using Student.Models; + +namespace Student.Controllers; + +public class StudentsController:Controller +{ + // 引入数据库文件 + private readonly StuDbContext _db; + public StudentsController(){ + _db = new StuDbContext(); + } + // 首页 + public IActionResult Index() + { + var list = _db.StuInfo.ToList(); + _db.SaveChanges(); + return View(list); + } + + // 添加 + public IActionResult Adds() + { + + return View(); + } + [HttpPost] + public IActionResult Adds(StuInfo input) + { + _db.StuInfo.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + + // 编辑 + public IActionResult Edit(int id) + { + var cont = _db.StuInfo.FristOrDefault(x=>x.Id==id) + return View(cont); + } +} + ... +``` +### Models-StuInfo +```js +namespace Student.Models; + +public class StuInfo{ + public int Id{get;set;} + public string Name{get;set;}=null!; + public int Age{get;set;} + public string Address{get;set;}=null!; + +} + + +``` +### Models-StuDbContext +```js +using Microsoft.EntityFrameworkCore; +namespace Student.Models; + +public class StuDbContext : DbContext +{ + public DbSet StuInfo{get;set;}=null!; + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ + base.OnConfiguring(optionsBuilder); + + // 连接数据库 + + var contionString = $"Server=.\\SQLEXPRESS;database=StudentInfo;uid=sa;pwd=123456;TrustServerCertificate=true"; + optionsBuilder.UseSqlServer(contionString); + } +} +``` + diff --git "a/\346\236\227\344\275\231\345\200\251/20241220\346\200\273\347\273\203\344\271\240.md" "b/\346\236\227\344\275\231\345\200\251/20241220\346\200\273\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..8fc4e2a14a8d2af73c6833590a385e79d48caa99 --- /dev/null +++ "b/\346\236\227\344\275\231\345\200\251/20241220\346\200\273\347\273\203\344\271\240.md" @@ -0,0 +1,112 @@ +## 练习 +### Controller +```js +using Microsoft.AspNetCore.Mvc; +// using Microsoft.EntityFrameworkCore; +using TwoNew.Models; +namespace TwoNew.Controllers; + +public class TwosController:Controller +{ + // 数据库 + private readonly TwoDbContext _db; + public TwosController() + { + _db=new TwoDbContext(); + } + + // 首页 + public IActionResult Index(string keyword) + { + if(string.IsNullOrEmpty(keyword)) + { + var list = _db.Book.ToList(); + _db.SaveChanges(); + return View(list); + } + var cont = _db.Book.Where(x=>x.BookName.Contains(keyword)||x.Author.Contains(keyword)).ToList(); + _db.SaveChanges(); + return View(cont); + } + // 添加 + public IActionResult Adds() + { + + return View(); + } + [HttpPost] + public IActionResult Adds(Book input) + { + _db.Book.Add(input); + _db.SaveChanges(); + return RedirectToAction("Index"); + } + // 编辑 + public IActionResult Edit(int id) + { + var cont = _db.Book.FirstOrDefault(x=>x.Id==id); + return View(cont); + } + [HttpPost] + public IActionResult Edit(Book input) + { + var cont = _db.Book.FirstOrDefault(x=>x.Id==input.Id); + if(cont==null){ + return NotFound(); + } + cont.BookName=input.BookName; + cont.Author=input.Author; + _db.SaveChanges(); + return RedirectToAction("Index"); + } + //删除 + public IActionResult Delete(int id) + { + var cont = _db.Book.FirstOrDefault(x=>x.Id==id); + + return View(cont); + } + public IActionResult DeleteOther(int id) + { + var cont = _db.Book.FirstOrDefault(x=>x.Id==id); + if(cont==null) + { + return NotFound(); + } + _db.Book.Remove(cont); + _db.SaveChanges(); + + return RedirectToAction("Index"); + + } + +} +``` + +### Models-实例表 +```js +namespace TwoNew.Models; + +public class Book{ + public int Id{get;set;} + public string BookName{get;set;}=null!; + public string Author{get;set;}=null!; +} +``` +### Models-sql +```js +using Microsoft.EntityFrameworkCore; +namespace TwoNew.Models; + +public class TwoDbContext:DbContext +{ + public DbSet Book{get;set;}=null!; + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + + var contionString =$"server=.\\SQLEXPRESS;database=MbBook;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(contionString); + } +} +```