From 7bd04c544bb5d08bab2399e1062a39b00514c8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=88=90=E8=B1=AA?= <1400383615@qq.com> Date: Sun, 15 Dec 2024 19:50:40 +0800 Subject: [PATCH] 20241215 --- ...272\224\347\224\250EntityFrameworkCore.md" | 57 ++++++++++++ ...da\350\241\250\350\276\276\345\274\217.md" | 88 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 "\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" create mode 100644 "\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241212-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lamda\350\241\250\350\276\276\345\274\217.md" diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" new file mode 100644 index 0000000..32e444e --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241209-\345\272\224\347\224\250EntityFrameworkCore.md" @@ -0,0 +1,57 @@ +1. 安装依赖包:`dotnet add package Microsoft.EntityFramewordCore.SqlServer` + +2. 定义数据库表模型 +```csharp +namespace Blog.Models; + +public class Blogs +{ + public required int ID {get; set;} + + public required string? Title {get; set;} + + public required string? Content {get; set;} + + public required string? Author {get; set;} +} +``` + +3. 定义数据库上下文 +```csharp +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs {get; set;} + + protected override void OnConfiguring(DbContextOptionsBuilder options) + + => options.UseSqlServer($"Server=.\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); + +} +``` + +4. 生成迁移文件:`dotnet ef migrations add XXX` +```csharp +// 需要dotnet-ef工具,还需要一个依赖包,名为Microsoft.EntityFrameworkCore.Design +dotnet tool install --global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef migrations add Init + +``` + +5. 将上一步生成的迁移文件应用到数据库中:`dotnet ef database update`(PS:需保证连接字符串正确无误,包括用户名、密码等,数据库打开,并允许远程连接) + +@startmindmap +* 控制器返回类型 + * 一般数据类型 直接返回如int、double、string、IEnumerable等数据类型 + * IActionResult类型 一个接口,用于返回HTTP状态信息,如200、301、401、404、500等 + * 视图 + * 重定向 + * ActionResult类型 将一般数据类型和HTTP状态信息混合使用 + * 特定于格式的操作结果:如JsonResult和ContentResult + * POCO(普通旧CLR对象) +@endmindmap + diff --git "a/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241212-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lamda\350\241\250\350\276\276\345\274\217.md" "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241212-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lamda\350\241\250\350\276\276\345\274\217.md" new file mode 100644 index 0000000..055c0fb --- /dev/null +++ "b/\346\235\250\346\210\220\350\261\252/\347\254\224\350\256\260/20241212-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lamda\350\241\250\350\276\276\345\274\217.md" @@ -0,0 +1,88 @@ +### Linq集成查询和Lamda表达式 + +1. First() FirstOrDefault() 获取集合中(符合条件的)第一个 + + + First() 这个如果没有获取到,则报错 + + FirstOrDefault() 没有获取到,则返回Null + +2. Single() SingleOrDefault() 获取集合中(符合条件)的其中一个 + + + Single() 这个如果没有获取到,则报错 + + SingleOrDefault() 没有获取到,则返回Null + +3. Where() 获取集合中符合条件的元素,将它们筛选出来放入一个新的集合中返回 + + + Where 查找符合条件的内容 + +4. Select() 返回指定内容 + + + Select() 返回指定内容 Select(x=>new {x.Id}) + +5. OrderBy() 按照指定内容排序 + + + OrderBy() 按照指定内容排序 + +6. OrderByDescending() 按照指定内容降序排序 + + + OrderByDescending() 按照指定内容降序排序 + +7. ThenBy() 按照指定内容排序,如果上一个排序相同,则按照这个排序 + + + ThenBy() 按照指定内容排序,如果上一个排序相同,则按照这个排序 + +8. ThenByDescending() 按照指定内容降序排序,如果上一个排序相同,则按照这个排序 + + + ThenByDescending() 按照指定内容降序排序,如果上一个排序相同,则按照这个排序 + +9. Skip() 跳过指定数量的元素 + + + Skip() 跳过指定数量的元素 + +10. Take() 获取指定数量的元素 + + + Take() 获取指定数量的元素 + +11. GroupBy() 按照指定内容分组 + + + GroupBy() 按照指定内容分组 + +12. Any() 判断集合中是否存在符合条件的元素 + + + Any() 判断集合中是否存在符合条件的元素 + +13. All() 判断集合中是否所有元素都符合条件 + + + All() 判断集合中是否所有元素都符合条件 + +14. Count() 获取集合中符合条件的元素数量 + + + Count() 获取集合中符合条件的元素数量 + +15. Sum() 获取集合中符合条件的元素的总和 + + + Sum() 获取集合中符合条件的元素的总和 + +16. Average() 获取集合中符合条件的元素的平均值 + + + Average() 获取集合中符合条件的元素的平均值 + +17. Min() 获取集合中符合条件的元素的最小值 + + + Min() 获取集合中符合条件的元素的最小值 + +18. Max() 获取集合中符合条件的元素的最大值 + + + Max() 获取集合中符合条件的元素的最大值 + +19. Contains() 判断集合中是否存在指定元素 + + + Contains() 判断集合中是否存在指定元素 + +20. Distinct() 去除集合中重复的元素 + + + Distinct() 去除集合中重复的元素 + +21. Concat() 合并两个集合 + + + Concat() 合并两个集合 + \ No newline at end of file -- Gitee