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 0000000000000000000000000000000000000000..a64287ebcee196da9c0ff415132fc8b963616622 --- /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 0000000000000000000000000000000000000000..c28405771c1cdd9a4fccbd810c71ebd5b4ddd953 --- /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 0000000000000000000000000000000000000000..3f67248718521478296248b18c18bf90fbe07222 --- /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 0000000000000000000000000000000000000000..1bb0057eb0707237047fe9b526b63823f3e86552 --- /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