From e981430a578826d81bc56855535d328d285229fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=B9=BF=E6=81=BA?= <1332727966@qq.com> Date: Sun, 25 Jan 2026 23:32:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20260119.md" | 19 ++++++++ .../20260121.md" | 44 +++++++++++++++++++ .../20260122.md" | 16 +++++++ .../20260123.md" | 10 +++++ 4 files changed, 89 insertions(+) create mode 100644 "\345\206\257\345\271\277\346\201\272/20260119.md" create mode 100644 "\345\206\257\345\271\277\346\201\272/20260121.md" create mode 100644 "\345\206\257\345\271\277\346\201\272/20260122.md" create mode 100644 "\345\206\257\345\271\277\346\201\272/20260123.md" diff --git "a/\345\206\257\345\271\277\346\201\272/20260119.md" "b/\345\206\257\345\271\277\346\201\272/20260119.md" new file mode 100644 index 0000000..a64287e --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260119.md" @@ -0,0 +1,19 @@ +// 数据库链接 + +using Microsoft.EntityFrameworkCore; +namespace ProductsSystem.Models; +public class AppDbContext : DbContext +{ + public DbSet products { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=Data.db;"); + } +} + + private readonly AppDbContext db; + public ProductController() + { + db = new AppDbContext(); + } +``` \ No newline at end of file diff --git "a/\345\206\257\345\271\277\346\201\272/20260121.md" "b/\345\206\257\345\271\277\346\201\272/20260121.md" new file mode 100644 index 0000000..c284057 --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260121.md" @@ -0,0 +1,44 @@ +```C# +// 功能实现 + // 新增商品 + public IActionResult Add() + { + return View(); + } + + [HttpPost] + public IActionResult AddSave(ProductsData product) + { + if (ModelState.IsValid) + { + db.products.Add(product); + db.SaveChanges(); + return Redirect("/Product/Index"); + } + return View(product); + } + // 编辑商品 + + public IActionResult Edit(int id) + + { + + var prooduct = db.products.Find(id); + + return View(prooduct); + + } + + [HttpPost] + public IActionResult EditSave(ProductsData product) + { + if (ModelState.IsValid) + { + db.products.Update(product); + db.SaveChanges(); + return Redirect("/Product/Index"); + } + return View(product); + + } +``` \ No newline at end of file diff --git "a/\345\206\257\345\271\277\346\201\272/20260122.md" "b/\345\206\257\345\271\277\346\201\272/20260122.md" new file mode 100644 index 0000000..3f67248 --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260122.md" @@ -0,0 +1,16 @@ +```C# + // 删除商品 + public IActionResult Delete(int id) + { + var product = db.products.Find(id); + return View(product); + } + [HttpPost] + public IActionResult DeleteSave(int id) + { + var product = db.products.Find(id); + db.products.Remove(product); + db.SaveChanges(); + return Redirect("/Product/Index"); + } +``` \ No newline at end of file diff --git "a/\345\206\257\345\271\277\346\201\272/20260123.md" "b/\345\206\257\345\271\277\346\201\272/20260123.md" new file mode 100644 index 0000000..1bb0057 --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260123.md" @@ -0,0 +1,10 @@ +``` C# +// 安装必要的NuGet包** +dotnet add package Microsoft.EntityFrameworkCore.Sqlite +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet add package Microsoft.EntityFrameworkCore.Tools + +// 迁移创建 +dotnet ef migrations add InitialCreate +dotnet ef database update TargetMigration +``` \ No newline at end of file -- Gitee