diff --git "a/\346\226\271\350\213\223\351\234\217/20260119.md" "b/\346\226\271\350\213\223\351\234\217/20260119.md" new file mode 100644 index 0000000000000000000000000000000000000000..d2f8851f6884c8e2a72f25f1c2e33af983bc154d --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260119.md" @@ -0,0 +1,41 @@ +# 笔记 + +1.创建模型类 + + 基本模型类:模型类通常放在Models文件夹中,是普通的C#类。 + 示例:Student.cs + + ```bash + namespace StudentManagementSystem.Models + { + public class Student + { + public int Id { get; set; } + public string Name { get; set; } + public int Age { get; set; } + public string Email { get; set; } + public DateTime EnrollmentDate { get; set; } + public string Major { get; set; } + } + } + ``` + +2. 什么是模型绑定? + + - 模型绑定是ASP.NET Core将HTTP请求数据自动映射到控制器动作参数的过程。 + + - 绑定来源: + - 路由数据(Route data) + - 查询字符串(Query string) + - 表单数据(Form data) + - JSON/XML请求体 + + - 绑定特性 + ```bash + // 指定绑定来源 + [FromQuery] // 从查询字符串绑定 + [FromRoute] // 从路由数据绑定 + [FromForm] // 从表单数据绑定 + [FromBody] // 从请求体绑定(JSON/XML) + [FromHeader] // 从请求头绑定 + [FromServices] // 从依赖注入容器绑定服务 \ No newline at end of file diff --git "a/\346\226\271\350\213\223\351\234\217/20260121.md" "b/\346\226\271\350\213\223\351\234\217/20260121.md" new file mode 100644 index 0000000000000000000000000000000000000000..8a1d65ecdd6c5997099edb6957837f6050527ca5 --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260121.md" @@ -0,0 +1,61 @@ +# 笔记 +1. EF Core的工作方式 + + - Code First:先写代码(模型),再生成数据库(我们采用的方式) + - Database First:已有数据库,根据数据库生成代码 + - Model First:先设计模型图,再生成代码和数据库 + +2. 迁移(Migration) + + - 安装EF Core工具(如果尚未安装)`dotnet tool install --global dotnet-ef` + + - 创建迁移`dotnet ef migrations add InitialCreate` + + - 应用迁移到数据库`dotnet ef database update` + + - 删除最近一次迁移`dotnet ef migrations remove` + + - 生成SQL脚本(不执行)`dotnet ef migrations script` + + - 更新到特定迁移`dotnet ef database update TargetMigration` + +3. 使用DbContext进行数据操作 + + - 新增(Create): + + ```bash + var student = new Student { Name = "张三", Age = 20 }; + context.Students.Add(student); + await context.SaveChangesAsync(); + ``` + + - 读取(Read): + + ```bash + var student = await context.Students.FindAsync(id); // 按主键查找 + var students = await context.Students.Where(s => s.Age > 18).ToListAsync(); + ``` + + - 更新(Update): + + ```bash + student.Name = "李四"; + context.Students.Update(student); + await context.SaveChangesAsync(); + ``` + + - 删除(Delete): + + ```bash + context.Students.Remove(student); + await context.SaveChangesAsync(); + + ``` + +- 状态 + + - Detached:未跟踪 + - Added:已添加,但未保存 + - Unchanged:未修改 + - Modified:已修改 + - Deleted:已删除 \ No newline at end of file diff --git "a/\346\226\271\350\213\223\351\234\217/20260122.md" "b/\346\226\271\350\213\223\351\234\217/20260122.md" new file mode 100644 index 0000000000000000000000000000000000000000..d83227b0dc2094f1139f85791209d200021b90a6 --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260122.md" @@ -0,0 +1,70 @@ +# 笔记 +1. 步骤1:安装2个包 + + 1. 安装EF Core SQLite提供程序(`Microsoft.EntityFrameworkCore.Sqlite`) + + - `dotnet add package Microsoft.EntityFrameworkCore.Sqlite -v 8.0` + + 2. 安装EF Core设计工具(用于迁移)(Microsoft.EntityFrameworkCore.Design) + + - `dotnet add package Microsoft.EntityFrameworkCore.Design -v 8.0` + +2. 步骤2:定义数据库上下文 + + 1. 定义数据库 + + - `public Dbset 名称{get;set}` + + 2. 配置数据库连接字符串 + +3. 步骤3:执行数据库迁移 + + 1. 2个先决条件 + + - 不能眼编译错误 + - 项目没有在运行,不在运行状态 + + 2. 命令 + + - `dotnet ef migrations add XXX` + + 3. 如果没有安装ef工具,使用如下命令 + + - `dotnet tool install --global dotnet-ef` + +4. 步骤4:将迁移文件更新应用到数据库 + + - `dotnet er database update` + +5. 步骤5:在控制器中使用数据库撒花姑娘下文进行CRUD + + - 新增(Create): + + ```bash + var student = new Student { Name = "张三", Age = 20 }; + context.Students.Add(student); + await context.SaveChangesAsync(); + ``` + + - 读取(Read): + + ```bash + var student = await context.Students.FindAsync(id); // 按主键查找 + var students = await context.Students.Where(s => s.Age > 18).ToListAsync(); + ``` + + - 更新(Update): + + ```bash + student.Name = "李四"; + context.Students.Update(student); + await context.SaveChangesAsync(); + ``` + + - 删除(Delete): + + ```bash + context.Students.Remove(student); + await context.SaveChangesAsync(); + + ``` \ No newline at end of file diff --git "a/\346\226\271\350\213\223\351\234\217/20260123.md" "b/\346\226\271\350\213\223\351\234\217/20260123.md" new file mode 100644 index 0000000000000000000000000000000000000000..4137f50bdc9d706bc36c016cc4087c34a21ae84a --- /dev/null +++ "b/\346\226\271\350\213\223\351\234\217/20260123.md" @@ -0,0 +1,20 @@ +# 笔记 +查找功能 + +```bash +public IActionResult Index(string keyword) + { + // 直接使用数据库表中的数据 + IEnumerable list = db.Products; + + // 当keyword不为空时,在数据库表中查找关键内容 + if (!string.IsNullOrEmpty(keyword)) + { + list = list.Where(p => p.ProductName.Contains(keyword) || p.Tag.Contains(keyword)); + } + return View(list); + } +``` + +### 练习 +无 \ No newline at end of file