From 2386869b52ab61b01c1a65f508522678068eebdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=A2=93=E5=90=9B?= <1559844100@qq.com> Date: Sun, 25 Jan 2026 13:11:54 +0000 Subject: [PATCH] =?UTF-8?q?=E5=90=B4=E6=A2=93=E5=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴梓君 <1559844100@qq.com> --- .../20260119-MVC\347\254\224\350\256\260.md" | 20 +++++++++ .../20260121-MVC\347\254\224\350\256\260.md" | 44 +++++++++++++++++++ .../20260122-MVC\347\254\224\350\256\260.md" | 16 +++++++ .../20260123-MVC\347\254\224\350\256\260.md" | 10 +++++ 4 files changed, 90 insertions(+) create mode 100644 "\345\220\264\346\242\223\345\220\233/20260119-MVC\347\254\224\350\256\260.md" create mode 100644 "\345\220\264\346\242\223\345\220\233/20260121-MVC\347\254\224\350\256\260.md" create mode 100644 "\345\220\264\346\242\223\345\220\233/20260122-MVC\347\254\224\350\256\260.md" create mode 100644 "\345\220\264\346\242\223\345\220\233/20260123-MVC\347\254\224\350\256\260.md" diff --git "a/\345\220\264\346\242\223\345\220\233/20260119-MVC\347\254\224\350\256\260.md" "b/\345\220\264\346\242\223\345\220\233/20260119-MVC\347\254\224\350\256\260.md" new file mode 100644 index 0000000..bb81f71 --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260119-MVC\347\254\224\350\256\260.md" @@ -0,0 +1,20 @@ +```C# +// 数据库链接 + +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\220\264\346\242\223\345\220\233/20260121-MVC\347\254\224\350\256\260.md" "b/\345\220\264\346\242\223\345\220\233/20260121-MVC\347\254\224\350\256\260.md" new file mode 100644 index 0000000..0abd221 --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260121-MVC\347\254\224\350\256\260.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\220\264\346\242\223\345\220\233/20260122-MVC\347\254\224\350\256\260.md" "b/\345\220\264\346\242\223\345\220\233/20260122-MVC\347\254\224\350\256\260.md" new file mode 100644 index 0000000..4030a8e --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260122-MVC\347\254\224\350\256\260.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\220\264\346\242\223\345\220\233/20260123-MVC\347\254\224\350\256\260.md" "b/\345\220\264\346\242\223\345\220\233/20260123-MVC\347\254\224\350\256\260.md" new file mode 100644 index 0000000..1bb0057 --- /dev/null +++ "b/\345\220\264\346\242\223\345\220\233/20260123-MVC\347\254\224\350\256\260.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