From b7b2bada3b62fe43784120de750269e7169e2260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E4=BC=9F?= <2113921291@qq.com> Date: Sun, 7 Jun 2026 11:36:35 +0000 Subject: [PATCH 1/4] ZY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王德伟 <2113921291@qq.com> --- .../20260601md.txt" | 5 +++++ .../20260603md.txt" | 5 +++++ .../20260604.md" | 9 ++++++++ .../20260605md.txt" | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 "\347\216\213\345\276\267\344\274\237/20260601md.txt" create mode 100644 "\347\216\213\345\276\267\344\274\237/20260603md.txt" create mode 100644 "\347\216\213\345\276\267\344\274\237/20260604.md" create mode 100644 "\347\216\213\345\276\267\344\274\237/20260605md.txt" diff --git "a/\347\216\213\345\276\267\344\274\237/20260601md.txt" "b/\347\216\213\345\276\267\344\274\237/20260601md.txt" new file mode 100644 index 0000000..9c2698f --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260601md.txt" @@ -0,0 +1,5 @@ +## 安装dotnet sdk + +- 访问 https://dotnet.microsoft.com/download/dotnet/8.0进行安装 + +- 验证安装使用dotnet --version \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260603md.txt" "b/\347\216\213\345\276\267\344\274\237/20260603md.txt" new file mode 100644 index 0000000..11d6454 --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260603md.txt" @@ -0,0 +1,5 @@ +## 项目创建 + +- 使用dotnet new webapi -n MyShopApi --use-controllers创建webapi项目 + +- 创建完成输入dotnet run运行 \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260604.md" "b/\347\216\213\345\276\267\344\274\237/20260604.md" new file mode 100644 index 0000000..1747aa7 --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260604.md" @@ -0,0 +1,9 @@ +## 项目需要用到的一蓝宝安装 + +- dotnet add package Microsoft.EntityFrameworkCore.Sqlite +- dotnet add package Microsoft.EntityFrameworkCore.Design +- 在命令后加上-v 8 , 安装8.0的版本 + +- dotnet-ef工具安装 + +- dotnet tool install --global dotnet-ef \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260605md.txt" "b/\347\216\213\345\276\267\344\274\237/20260605md.txt" new file mode 100644 index 0000000..bdfb6fb --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260605md.txt" @@ -0,0 +1,21 @@ +## 增删改查连接数据库实现 + +``` C# +using Microsoft.AspNetCore.Mvc; +using dec.Models; +using dec.Data; +using Microsoft.EntityFrameworkCore; + + +namespace dec.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class CategoriesController : ControllerBase +{ + private readonly AppDbcontext _context; + + public CategoriesController(AppDbcontext context) + { + _context = context; + } -- Gitee From f9eb12e8e3a4034c6d6c8af009baa65ddff9b025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E4=BC=9F?= <2113921291@qq.com> Date: Sun, 7 Jun 2026 11:38:09 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E4=BC=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20260601md.txt" | 5 ----- .../20260603md.txt" | 5 ----- .../20260604.md" | 9 -------- .../20260605md.txt" | 21 ------------------- 4 files changed, 40 deletions(-) delete mode 100644 "\347\216\213\345\276\267\344\274\237/20260601md.txt" delete mode 100644 "\347\216\213\345\276\267\344\274\237/20260603md.txt" delete mode 100644 "\347\216\213\345\276\267\344\274\237/20260604.md" delete mode 100644 "\347\216\213\345\276\267\344\274\237/20260605md.txt" diff --git "a/\347\216\213\345\276\267\344\274\237/20260601md.txt" "b/\347\216\213\345\276\267\344\274\237/20260601md.txt" deleted file mode 100644 index 9c2698f..0000000 --- "a/\347\216\213\345\276\267\344\274\237/20260601md.txt" +++ /dev/null @@ -1,5 +0,0 @@ -## 安装dotnet sdk - -- 访问 https://dotnet.microsoft.com/download/dotnet/8.0进行安装 - -- 验证安装使用dotnet --version \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260603md.txt" "b/\347\216\213\345\276\267\344\274\237/20260603md.txt" deleted file mode 100644 index 11d6454..0000000 --- "a/\347\216\213\345\276\267\344\274\237/20260603md.txt" +++ /dev/null @@ -1,5 +0,0 @@ -## 项目创建 - -- 使用dotnet new webapi -n MyShopApi --use-controllers创建webapi项目 - -- 创建完成输入dotnet run运行 \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260604.md" "b/\347\216\213\345\276\267\344\274\237/20260604.md" deleted file mode 100644 index 1747aa7..0000000 --- "a/\347\216\213\345\276\267\344\274\237/20260604.md" +++ /dev/null @@ -1,9 +0,0 @@ -## 项目需要用到的一蓝宝安装 - -- dotnet add package Microsoft.EntityFrameworkCore.Sqlite -- dotnet add package Microsoft.EntityFrameworkCore.Design -- 在命令后加上-v 8 , 安装8.0的版本 - -- dotnet-ef工具安装 - -- dotnet tool install --global dotnet-ef \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260605md.txt" "b/\347\216\213\345\276\267\344\274\237/20260605md.txt" deleted file mode 100644 index bdfb6fb..0000000 --- "a/\347\216\213\345\276\267\344\274\237/20260605md.txt" +++ /dev/null @@ -1,21 +0,0 @@ -## 增删改查连接数据库实现 - -``` C# -using Microsoft.AspNetCore.Mvc; -using dec.Models; -using dec.Data; -using Microsoft.EntityFrameworkCore; - - -namespace dec.Controllers; - -[ApiController] -[Route("api/[controller]")] -public class CategoriesController : ControllerBase -{ - private readonly AppDbcontext _context; - - public CategoriesController(AppDbcontext context) - { - _context = context; - } -- Gitee From faf503e5f1fddbfbcbce2dbeab544a9b284218f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E4=BC=9F?= <2113921291@qq.com> Date: Sun, 7 Jun 2026 11:39:03 +0000 Subject: [PATCH 3/4] ZY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王德伟 <2113921291@qq.com> --- .../20260601.md" | 5 +++++ .../20260603.md" | 5 +++++ .../20260604.md" | 9 ++++++++ .../20260605.md" | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 "\347\216\213\345\276\267\344\274\237/20260601.md" create mode 100644 "\347\216\213\345\276\267\344\274\237/20260603.md" create mode 100644 "\347\216\213\345\276\267\344\274\237/20260604.md" create mode 100644 "\347\216\213\345\276\267\344\274\237/20260605.md" diff --git "a/\347\216\213\345\276\267\344\274\237/20260601.md" "b/\347\216\213\345\276\267\344\274\237/20260601.md" new file mode 100644 index 0000000..9c2698f --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260601.md" @@ -0,0 +1,5 @@ +## 安装dotnet sdk + +- 访问 https://dotnet.microsoft.com/download/dotnet/8.0进行安装 + +- 验证安装使用dotnet --version \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260603.md" "b/\347\216\213\345\276\267\344\274\237/20260603.md" new file mode 100644 index 0000000..11d6454 --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260603.md" @@ -0,0 +1,5 @@ +## 项目创建 + +- 使用dotnet new webapi -n MyShopApi --use-controllers创建webapi项目 + +- 创建完成输入dotnet run运行 \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260604.md" "b/\347\216\213\345\276\267\344\274\237/20260604.md" new file mode 100644 index 0000000..1747aa7 --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260604.md" @@ -0,0 +1,9 @@ +## 项目需要用到的一蓝宝安装 + +- dotnet add package Microsoft.EntityFrameworkCore.Sqlite +- dotnet add package Microsoft.EntityFrameworkCore.Design +- 在命令后加上-v 8 , 安装8.0的版本 + +- dotnet-ef工具安装 + +- dotnet tool install --global dotnet-ef \ No newline at end of file diff --git "a/\347\216\213\345\276\267\344\274\237/20260605.md" "b/\347\216\213\345\276\267\344\274\237/20260605.md" new file mode 100644 index 0000000..bdfb6fb --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20260605.md" @@ -0,0 +1,21 @@ +## 增删改查连接数据库实现 + +``` C# +using Microsoft.AspNetCore.Mvc; +using dec.Models; +using dec.Data; +using Microsoft.EntityFrameworkCore; + + +namespace dec.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class CategoriesController : ControllerBase +{ + private readonly AppDbcontext _context; + + public CategoriesController(AppDbcontext context) + { + _context = context; + } -- Gitee From dfd8d3def55b1200edc6e357b1dca65dadd1cbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E4=BC=9F?= <2113921291@qq.com> Date: Sun, 14 Jun 2026 13:32:16 +0000 Subject: [PATCH 4/4] ZY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王德伟 <2113921291@qq.com> --- .../20060608.md" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "\347\216\213\345\276\267\344\274\237/20060608.md" diff --git "a/\347\216\213\345\276\267\344\274\237/20060608.md" "b/\347\216\213\345\276\267\344\274\237/20060608.md" new file mode 100644 index 0000000..a46d4e2 --- /dev/null +++ "b/\347\216\213\345\276\267\344\274\237/20060608.md" @@ -0,0 +1,88 @@ +### 代码定义示例 + +```csharp +// 主体实体(“一”端) +public class Blog +{ + public int Id { get; set; } + // 集合导航:一个 Blog 有多个 Post + public ICollection Posts { get; set; } = new List(); +} + +// 依赖实体(“多”端) +public class Post +{ + public int Id { get; set; } + public int BlogId { get; set; } // 外键属性 + // 引用导航:一个 Post 属于一个 Blog + public Blog Blog { get; set; } = null!; +} +``` + +- `Blog.Posts` 为集合导航,表示“一个博客下有多个文章”; +- `Post.Blog` 为引用导航,表示“每篇文章属于一个博客”。 +- 外键 `Post.BlogId` + 引用导航 `Post.Blog` 共同建立了关系约束。 + +### 🔧 关系配置方式 + +EF Core 默认**约定大于配置**。以下面的实体为例,EF 能自动推断这是一对多关系: + +```csharp +public class Article { + public long Id { get; set; } + public List Comments { get; set; } +} +public class Comment { + public long Id { get; set; } + public long ArticleId { get; set; } + public Article Article { get; set; } +} +``` + +如果需要更明确的控制,或在约定不成立时手动配置,可以使用 **Fluent API**: + +```csharp +protected override void OnModelCreating(ModelBuilder modelBuilder) +{ + modelBuilder.Entity
() + .HasMany(a => a.Comments) // Article 有多个 Comments + .WithOne(c => c.Article) // Comment 有一个 Article + .HasForeignKey(c => c.ArticleId); +} +``` + +- `HasMany()` + `WithOne()` 组合定义一对多; +- 需先用 `Has` 方向确定主体,再用 `With` 方向确认依赖; +- 最后 `HasForeignKey()` 指定外键字段。 + +### 数据加载策略 + +导航属性默认**不自动加载**,EF Core 提供三种加载策略: + +1. **贪婪加载 (Eager Loading)**:一次性加载主体与所有关联数据 + + ```csharp + var blogs = context.Blogs + .Include(b => b.Posts) // 加载集合导航 + .ThenInclude(p => p.Comments) // 继续深层加载 + .ToList(); + ``` + + `Include()` 减少后续 SQL 请求,**显著提升查询性能**;深层嵌套时使用 `ThenInclude()`。 + +2. **显式加载 (Explicit Loading)**:按需手动加载 + + ```csharp + context.Entry(blog).Collection(b => b.Posts).Load(); + ``` + +3. **延迟加载 (Lazy Loading)**:访问时自动触发(需安装 `Microsoft.EntityFrameworkCore.Proxies`,配置 + +`UseLazyLoadingProxies`,导航属性设为 `virtual`) + +```csharp +protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) +{ + optionsBuilder.UseLazyLoadingProxies(); +} +``` \ No newline at end of file -- Gitee