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 0000000000000000000000000000000000000000..bb81f714086fa5b91c9bbf04b105acfa9aee3985 --- /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 0000000000000000000000000000000000000000..0abd2215d822db1d15759539b367512a8e16e5fe --- /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 0000000000000000000000000000000000000000..4030a8eafeefb0dd34539954c2ea27cf32f2bb5c --- /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 0000000000000000000000000000000000000000..1bb0057eb0707237047fe9b526b63823f3e86552 --- /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