From 607306f9a7a8810a0a07382591c0bfa73ae65bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E9=B8=BF=E9=9B=AF?= <3202800051@qq.com> Date: Sun, 15 Dec 2024 20:42:26 +0800 Subject: [PATCH] 1213 --- ...1\345\222\214\346\220\234\347\264\242..md" | 164 ++++++++++ .../20241212-linq.md" | 124 ++++++++ .../20241213-\346\250\241\345\236\213.md" | 281 ++++++++++++++++++ 3 files changed, 569 insertions(+) create mode 100644 "\350\214\203\351\270\277\351\233\257/20241210-\347\274\226\350\276\221\345\222\214\346\220\234\347\264\242..md" create mode 100644 "\350\214\203\351\270\277\351\233\257/20241212-linq.md" create mode 100644 "\350\214\203\351\270\277\351\233\257/20241213-\346\250\241\345\236\213.md" diff --git "a/\350\214\203\351\270\277\351\233\257/20241210-\347\274\226\350\276\221\345\222\214\346\220\234\347\264\242..md" "b/\350\214\203\351\270\277\351\233\257/20241210-\347\274\226\350\276\221\345\222\214\346\220\234\347\264\242..md" new file mode 100644 index 0000000..a055f4f --- /dev/null +++ "b/\350\214\203\351\270\277\351\233\257/20241210-\347\274\226\350\276\221\345\222\214\346\220\234\347\264\242..md" @@ -0,0 +1,164 @@ + +### 1. 创建模型 + +首先,我们定义一个简单的笔记模型: + +```csharp +public class Note +{ + public int Id { get; set; } + public string Title { get; set; } + public string Content { get; set; } +} +``` + +### 2. 创建数据库上下文 + +使用Entity Framework Core创建数据库上下文: + +```csharp +using Microsoft.EntityFrameworkCore; + +public class AppDbContext : DbContext +{ + public AppDbContext(DbContextOptions options) : base(options) { } + + public DbSet Notes { get; set; } +} +``` + +### 3. 创建控制器 + +接下来,创建`NotesController`,处理编辑和搜索功能: + +```csharp +using Microsoft.AspNetCore.Mvc; +using System.Linq; + +public class NotesController : Controller +{ + private readonly AppDbContext _context; + + public NotesController(AppDbContext context) + { + _context = context; + } + + // 搜索笔记 + public IActionResult Search(string searchString) + { + var notes = from n in _context.Notes + select n; + + if (!string.IsNullOrEmpty(searchString)) + { + notes = notes.Where(s => s.Title.Contains(searchString) || s.Content.Contains(searchString)); + } + + return View(notes.ToList()); + } + + // 编辑笔记 + public IActionResult Edit(int id) + { + var note = _context.Notes.Find(id); + if (note == null) + { + return NotFound(); + } + return View(note); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public IActionResult Edit(Note note) + { + if (ModelState.IsValid) + { + _context.Update(note); + _context.SaveChanges(); + return RedirectToAction(nameof(Index)); // 返回到索引或成功的页面 + } + return View(note); + } + + // 其他动作,比如 Index, Create, Delete等... +} +``` + +### 4. 创建视图 + +**搜索视图 Search.cshtml** + +```html +@model IEnumerable + +

搜索笔记

+ +
+ + +
+ + + + + + + + + + + @foreach (var note in Model) + { + + + + + + } + +
标题内容操作
@note.Title@note.Content + 编辑 +
+``` + +**编辑视图 Edit.cshtml** + +```html +@model Note + +

编辑笔记

+ +
+ +
+ + + +
+
+ + + +
+ +
+ +返回 +``` + +### 5. 配置路由和依赖注入 + +在`Startup.cs`中,添加数据库上下文并配置路由: + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddDbContext(options => + options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + + services.AddControllersWithViews(); +} +``` + diff --git "a/\350\214\203\351\270\277\351\233\257/20241212-linq.md" "b/\350\214\203\351\270\277\351\233\257/20241212-linq.md" new file mode 100644 index 0000000..9798f90 --- /dev/null +++ "b/\350\214\203\351\270\277\351\233\257/20241212-linq.md" @@ -0,0 +1,124 @@ + +### 1. 定义模型 + +定义一个简单的模型`Note`来代表Markdown笔记,包含创建时间、文件名和内容等属性: + +```csharp +public class Note +{ + public int Id { get; set; } + public string Title { get; set; } // 文件名 + public string Content { get; set; } // 内容 + public DateTime CreatedAt { get; set; } // 创建时间 +} +``` + +### 2. 添加数据库上下文 + +确保您已经设置数据库上下文,以便使用Entity Framework Core来管理数据: + +```csharp +using Microsoft.EntityFrameworkCore; + +public class AppDbContext : DbContext +{ + public AppDbContext(DbContextOptions options) : base(options) { } + + public DbSet Notes { get; set; } // DbSet用于CRUD操作 +} +``` + +### 3. LINQ查询 + +接下来,您可以使用LINQ来查询笔记。假设您想查询最近一个小时内创建的所有笔记,以及查询特定文件名的笔记。如下是可能的查询示例: + +```csharp +using System; +using System.Linq; + +public class NotesService +{ + private readonly AppDbContext _context; + + public NotesService(AppDbContext context) + { + _context = context; + } + + public void FindRecentNotes() + { + var oneHourAgo = DateTime.Now.AddHours(-1); + + var recentNotes = _context.Notes + .Where(n => n.CreatedAt >= oneHourAgo) + .ToList(); // 获取最近一小时内创建的笔记 + + foreach (var note in recentNotes) + { + Console.WriteLine($"{note.CreatedAt}: {note.Title}"); + } + } + + public void FindNotesByFileName(string fileName) + { + // 假设 fileName 是 "20241213-模型.md" + var specificNotes = _context.Notes + .Where(n => n.Title.Equals(fileName, StringComparison.OrdinalIgnoreCase)) + .ToList(); // 查找特定文件名的笔记 + + foreach (var note in specificNotes) + { + Console.WriteLine($"{note.CreatedAt}: {note.Title}"); + } + } +} +``` + +### 4. 控制器示例 + +将上述服务整合到控制器中,实现API或MVC逻辑: + +```csharp +using Microsoft.AspNetCore.Mvc; + +public class NotesController : Controller +{ + private readonly NotesService _notesService; + + public NotesController(NotesService notesService) + { + _notesService = notesService; + } + + // 获取最近的笔记 + public IActionResult Recent() + { + _notesService.FindRecentNotes(); + return View(); // 返回相应视图 + } + + // 根据文件名查询笔记 + public IActionResult SearchByFileName(string fileName) + { + _notesService.FindNotesByFileName(fileName); + return View(); // 返回相应视图 + } +} +``` + +### 5. 视图示例 + +在控制器的视图中展示笔记,例如: + +```html +@model IEnumerable + +

最近的笔记

+
    + @foreach (var note in Model) + { +
  • @note.CreatedAt - @note.Title
  • + } +
+``` + diff --git "a/\350\214\203\351\270\277\351\233\257/20241213-\346\250\241\345\236\213.md" "b/\350\214\203\351\270\277\351\233\257/20241213-\346\250\241\345\236\213.md" new file mode 100644 index 0000000..4ce3c74 --- /dev/null +++ "b/\350\214\203\351\270\277\351\233\257/20241213-\346\250\241\345\236\213.md" @@ -0,0 +1,281 @@ + + +### 1. 创建模型 + +首先,定义一个`Note`模型类来表示笔记的内容: + +```csharp +public class Note +{ + public int Id { get; set; } // 主键 + public string Title { get; set; } // 笔记的标题 + public string Content { get; set; } // 笔记的内容 + public DateTime CreatedAt { get; set; } = DateTime.Now; // 创建时间 + public DateTime UpdatedAt { get; set; } = DateTime.Now; // 更新时间 +} +``` + +### 2. 创建数据库上下文 + +接下来,创建一个上下文类,用于与数据库交互: + +```csharp +using Microsoft.EntityFrameworkCore; + +public class AppDbContext : DbContext +{ + public AppDbContext(DbContextOptions options) : base(options) { } + + public DbSet Notes { get; set; } // DbSet用于CRUD操作 +} +``` + +### 3. 配置数据库连接 + +在`appsettings.json`中配置数据库连接字符串,例如使用SQLite或SQL Server: + +```json +{ + "ConnectionStrings": { + "DefaultConnection": "Data Source=notes.db" // SQLite示例 + // "DefaultConnection": "Server=yourserver;Database=yourdb;User Id=yourusername;Password=yourpassword;" + }, + // 其他配置... +} +``` + +### 4. 配置服务 + +在`Startup.cs`中配置服务和依赖注入: + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddDbContext(options => + options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); // 配置使用SQLite + + services.AddControllersWithViews(); // 添加MVC支持 +} +``` + +### 5. 创建控制器 + +接下来,创建一个控制器来处理笔记的逻辑: + +```csharp +using Microsoft.AspNetCore.Mvc; +using System.Linq; + +public class NotesController : Controller +{ + private readonly AppDbContext _context; + + public NotesController(AppDbContext context) + { + _context = context; + } + + // GET: Notes + public IActionResult Index() + { + var notes = _context.Notes.ToList(); + return View(notes); // 返回笔记列表视图 + } + + // GET: Notes/Create + public IActionResult Create() + { + return View(); // 返回创建笔记视图 + } + + // POST: Notes/Create + [HttpPost] + [ValidateAntiForgeryToken] + public IActionResult Create(Note note) + { + if (ModelState.IsValid) + { + _context.Notes.Add(note); + _context.SaveChanges(); + return RedirectToAction(nameof(Index)); // 创建成功后重定向到Index + } + return View(note); // 验证失败返回创建视图 + } + + // GET: Notes/Edit/5 + public IActionResult Edit(int id) + { + var note = _context.Notes.Find(id); + if (note == null) + { + return NotFound(); + } + return View(note); // 返回编辑视图 + } + + // POST: Notes/Edit/5 + [HttpPost] + [ValidateAntiForgeryToken] + public IActionResult Edit(Note note) + { + if (ModelState.IsValid) + { + _context.Update(note); + _context.SaveChanges(); + return RedirectToAction(nameof(Index)); // 编辑成功后重定向到Index + } + return View(note); // 验证失败返回编辑视图 + } + + // GET: Notes/Delete/5 + public IActionResult Delete(int id) + { + var note = _context.Notes.Find(id); + if (note == null) + { + return NotFound(); + } + return View(note); // 返回删除视图 + } + + // POST: Notes/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public IActionResult DeleteConfirmed(int id) + { + var note = _context.Notes.Find(id); + if (note != null) + { + _context.Notes.Remove(note); + _context.SaveChanges(); + } + return RedirectToAction(nameof(Index)); // 删除成功后重定向到Index + } +} +``` + +### 6. 创建视图 + +接下来,为每个操作创建相应的视图。以下是`Index`, `Create`, `Edit`, 和`Delete`视图的示例。 + +**Index.cshtml** - 显示所有笔记: + +```html +@model IEnumerable + +

笔记列表

+ +

+ 创建新笔记 +

+ + + + + + + + + + + + @foreach (var note in Model) + { + + + + + + + } + +
标题内容创建时间操作
@note.Title@note.Content@note.CreatedAt + 编辑 | + 删除 +
+``` + +**Create.cshtml** - 创建笔记: + +```html +@model Note + +

创建笔记

+ +
+
+ + + +
+
+ + + +
+ +
+ +返回 +``` + +**Edit.cshtml** - 编辑笔记: + +```html +@model Note + +

编辑笔记

+ +
+ +
+ + + +
+
+ + + +
+ +
+ +返回 +``` + +**Delete.cshtml** - 删除笔记: + +```html +@model Note + +

确认删除笔记

+ +
+

笔记

+
+
+
标题
+
@Model.Title
+
内容
+
@Model.Content
+
+
+ +
+ + +
+ +返回 +``` + +### 7. 数据迁移 + +在终端中运行以下命令以创建数据库和数据表: + +```bash +dotnet ef migrations add InitialCreate +dotnet ef database update +``` + -- Gitee