diff --git "a/\351\273\204\351\233\252\350\212\254/20241216-\350\277\236\346\216\245\345\256\236\351\231\205\346\225\260\346\215\256\345\272\223.md" "b/\351\273\204\351\233\252\350\212\254/20241216-\350\277\236\346\216\245\345\256\236\351\231\205\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000000000000000000000000000000000000..90764710795b951df96f2725f83f6620372864d2 --- /dev/null +++ "b/\351\273\204\351\233\252\350\212\254/20241216-\350\277\236\346\216\245\345\256\236\351\231\205\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,69 @@ +## 连接实际数据库 +### 数据库上下文初始化 +#### 使用[有参数]的方案 +1. 在Program.cs文件中添加连接数据库字符串 +```cs +var connectionString = $"Server=.;Database=MdBlog;uid=sa;pwd=123456;TrustServerCertificate=true"; + +builder.Services.AddDbContext(opt => +{ + opt.UseSqlServer(connectionString); +}); +builder.Services.AddScoped(); +``` +2. 在对应的Models文件中,不需要重写OnConfiguring的代码,只需要集成base的代码 +```cs +public BlogDbContext(DbContextOptions options) : base(options) +{ +} +``` + +#### 使用[无参数]的方案 +1. 在Program.cs文件中不需要填写任何的代码 +2. 在对应的Models文件中,需要有重写OnConfiguring的代码,继承base的代码则不再需要 +3. 将连接数据库的字符串代码写入重写OnConfiguring的代码中 +```cs +protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) +{ + base.OnConfiguring(optionsBuilder); + + var connectionString = $"Server=.;Database=MdBlog;uid=sa;pwd=123456;TrustServerCertificate=true"; + optionsBuilder.UseSqlServer(connectionString); +} +``` + +### 连接数据库 +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("连接符", 字符串数组/集合/对象数组) + +```cs +List fruits = new List { "Apple", "Banana", "Cherry" }; +string result = String.Join(", ", fruits); +Console.WriteLine(result); // 输出: Apple, Banana, Cherry +``` \ No newline at end of file diff --git "a/\351\273\204\351\233\252\350\212\254/20241218-\350\277\201\347\247\273\346\226\207\344\273\266.md" "b/\351\273\204\351\233\252\350\212\254/20241218-\350\277\201\347\247\273\346\226\207\344\273\266.md" new file mode 100644 index 0000000000000000000000000000000000000000..9a8382aa3c7541a27abc07a24704ef48b3b38fe4 --- /dev/null +++ "b/\351\273\204\351\233\252\350\212\254/20241218-\350\277\201\347\247\273\346\226\207\344\273\266.md" @@ -0,0 +1,11 @@ +## 一、生成迁移文件 +命令:`dotnet ef migrations add FirstInit` + +### 前提条件 +1. 需要一个Design的依赖包,安装命令:`dotnet add package Microsoft.EntityFrameworkCore.Design` +2. 需要一个工具包,这个工具是`dotnet-ef`,安装全局命令:`dotnet tool install --global dotnet-ef` +3. 程序不能有编译错误,可以使用`dotnet build`命令排查编译错误 +4. 程序不能处于运行状态 + +## 二、将生成的迁移文件同步到数据库 +命令:`dotnet ef database update` \ No newline at end of file diff --git "a/\351\273\204\351\233\252\350\212\254/20241220-\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\351\273\204\351\233\252\350\212\254/20241220-\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..be02d091f1bca7d39a6b98eb293cb2c042f5b23f --- /dev/null +++ "b/\351\273\204\351\233\252\350\212\254/20241220-\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,109 @@ +## 增 +```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 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 +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 +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