diff --git "a/\350\256\270\346\265\267\346\200\241/\344\275\234\344\270\232\346\210\252\345\233\276/\344\273\243\347\240\201\345\233\276.png" "b/\350\256\270\346\265\267\346\200\241/\344\275\234\344\270\232\346\210\252\345\233\276/\344\273\243\347\240\201\345\233\276.png" new file mode 100644 index 0000000000000000000000000000000000000000..6efb20caaf1b5d4dfe8740adb78f2389ee36258e Binary files /dev/null and "b/\350\256\270\346\265\267\346\200\241/\344\275\234\344\270\232\346\210\252\345\233\276/\344\273\243\347\240\201\345\233\276.png" differ diff --git "a/\350\256\270\346\265\267\346\200\241/\344\275\234\344\270\232\346\210\252\345\233\276/\346\225\210\346\236\234\345\233\276.jpg" "b/\350\256\270\346\265\267\346\200\241/\344\275\234\344\270\232\346\210\252\345\233\276/\346\225\210\346\236\234\345\233\276.jpg" new file mode 100644 index 0000000000000000000000000000000000000000..864e2ce114c524c3d7c22f27c0e3e8f75eef9beb Binary files /dev/null and "b/\350\256\270\346\265\267\346\200\241/\344\275\234\344\270\232\346\210\252\345\233\276/\346\225\210\346\236\234\345\233\276.jpg" differ diff --git "a/\350\256\270\346\265\267\346\200\241/\350\257\276\345\240\202\347\254\224\350\256\260/20241719-\350\277\236\346\216\245\345\220\214\346\255\245\346\225\260\346\215\256\345\272\223\345\222\214\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\350\256\270\346\265\267\346\200\241/\350\257\276\345\240\202\347\254\224\350\256\260/20241719-\350\277\236\346\216\245\345\220\214\346\255\245\346\225\260\346\215\256\345\272\223\345\222\214\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..91decd8826b9b033f63dcd580c69af93546b476e --- /dev/null +++ "b/\350\256\270\346\265\267\346\200\241/\350\257\276\345\240\202\347\254\224\350\256\260/20241719-\350\277\236\346\216\245\345\220\214\346\255\245\346\225\260\346\215\256\345\272\223\345\222\214\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,116 @@ +``` +## 3. 定义数据库上下文 +- 在Models中创建 BlogDbContext.cs文件 +```c# +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; } = null!; + + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=SK-20240829DIVS\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +``` +假设你有一个 `Blog` 类,并且创建了 `StudentDbContext`,步骤如下: + +1. **安装 EF Core 工具**: + + ```bash + dotnet tool install --global dotnet-ef + ``` + +2. **添加 EF Core Design 包**: + + ```bash + dotnet add package Microsoft.EntityFrameworkCore.Design + ``` +``` +安装EntityFrameworkCore,命令如下 +dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +创建实体类型(数据模型) + +创建数据库上下文,配置好 + + +进行数据迁移(它的成功有2个前提:1、程序不能有编译错误;2、程序必须停止运行),命令如下: + +// 需要dotnet-ef工具,还需要什么一个依赖包,名为Microsoft.EntityFrameworkCore.Design +dotnet tool install --global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef migrations add Init + +将生成的数据迁移文件,同步更新到数据库,命令如下: +dotnet ef database update +``` +public class StudentsController : Controller +{ + + private readonly StudentDbContext _db; + + public StudentsController() + { + _db = new StudentDbContext(); + } + public IActionResult Index() + { + // 从数据库传数据到Index里面 + var list = _db.Students.ToList(); + return View(list); + } + + public IActionResult Delete() + { + return View(); + } + + public IActionResult Edit() + { + return View(); + } + public IActionResult Create() + { + return View(); + } +} +``` +## 2. Views +- Student => Index.cshtml +``` +``` +@model List; +
+ @* form是查询的,新增用a标签 *@ +
+
+ 新增 + + + + + + + + + @foreach(var item in @Model) + { + + + + + + + } +
编号姓名年龄操作
@item.Id@item.StudentName@item.Age + 编辑 + 删除 +
+
+``` \ No newline at end of file