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 0000000000000000000000000000000000000000..a46d4e20d7b592b85555bcabb8d272f0483bea1f --- /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 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 0000000000000000000000000000000000000000..9c2698f727885a3e8823c2da25aaee74bbfd1c48 --- /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 0000000000000000000000000000000000000000..11d645439784104c340c4d8ab11104eddbd4f538 --- /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 0000000000000000000000000000000000000000..1747aa7aa3dd5e9ad89d69255b2c0274f9fb6b02 --- /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 0000000000000000000000000000000000000000..bdfb6fbb91e331b9e30858f584f6f2f8009815de --- /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; + }