diff --git "a/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.09\347\254\224\350\256\260.md" "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.09\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..6dd37f0f07acbfc5fc3aaa34ffaa2448d2ff8de0 --- /dev/null +++ "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.09\347\254\224\350\256\260.md" @@ -0,0 +1,56 @@ +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 \ No newline at end of file diff --git "a/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.12\347\254\224\350\256\260.md" "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.12\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..055c0fb52b9204743605adf9959b50d497b3ff46 --- /dev/null +++ "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.12\347\254\224\350\256\260.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