diff --git "a/\346\261\237\347\246\271\350\276\260/20260119-mvc\347\254\224\350\256\260.md" "b/\346\261\237\347\246\271\350\276\260/20260119-mvc\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..ba45e78b1f513a7f960c8f58c2204f5707ec9b98 --- /dev/null +++ "b/\346\261\237\347\246\271\350\276\260/20260119-mvc\347\254\224\350\256\260.md" @@ -0,0 +1,20 @@ +### 笔记 + +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; } + } + } \ No newline at end of file diff --git "a/\346\261\237\347\246\271\350\276\260/20260121-mvc\347\254\224\350\256\260.md" "b/\346\261\237\347\246\271\350\276\260/20260121-mvc\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..5b04a2b7f34b3431c54ddc0a8bda2f3e338d2ecf --- /dev/null +++ "b/\346\261\237\347\246\271\350\276\260/20260121-mvc\347\254\224\350\256\260.md" @@ -0,0 +1,30 @@ +### 笔记 +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(); + ``` \ No newline at end of file diff --git "a/\346\261\237\347\246\271\350\276\260/20260122-mvc\347\254\224\350\256\260.md" "b/\346\261\237\347\246\271\350\276\260/20260122-mvc\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..40878ae959e1ba043cd3fd484e936380eedc42c9 --- /dev/null +++ "b/\346\261\237\347\246\271\350\276\260/20260122-mvc\347\254\224\350\256\260.md" @@ -0,0 +1,47 @@ +### 笔记 +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(); + ``` \ No newline at end of file diff --git "a/\346\261\237\347\246\271\350\276\260/20260123-mvc\347\254\224\350\256\260.md" "b/\346\261\237\347\246\271\350\276\260/20260123-mvc\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..7f0cd85113a781072b78a20440391f03e3e8ba26 --- /dev/null +++ "b/\346\261\237\347\246\271\350\276\260/20260123-mvc\347\254\224\350\256\260.md" @@ -0,0 +1,17 @@ +### 笔记 +查找功能 + +```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