From 0d186a96633b3d323737103551b216109349bb7a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 9 Jun 2024 22:21:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...to\347\232\204\345\245\275\345\244\204.md" | 166 ++++++++++++++++++ ...15\347\275\256\346\216\245\345\217\243.md" | 160 +++++++++++++++++ ...77\347\224\250\346\226\271\346\263\225.md" | 97 ++++++++++ ...re\347\232\204\347\256\200\344\273\213.md" | 66 +++++++ 4 files changed, 489 insertions(+) create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.6.3Dto\347\232\204\345\245\275\345\244\204.md" create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.6.4\351\205\215\347\275\256\346\216\245\345\217\243.md" create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.6.7EF Core\347\232\204\344\270\244\347\247\215\344\275\277\347\224\250\346\226\271\346\263\225.md" create mode 100644 "\351\253\230\344\277\212\346\235\260/2024.6.EF Core\347\232\204\347\256\200\344\273\213.md" diff --git "a/\351\253\230\344\277\212\346\235\260/2024.6.3Dto\347\232\204\345\245\275\345\244\204.md" "b/\351\253\230\344\277\212\346\235\260/2024.6.3Dto\347\232\204\345\245\275\345\244\204.md" new file mode 100644 index 0000000..6216475 --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.6.3Dto\347\232\204\345\245\275\345\244\204.md" @@ -0,0 +1,166 @@ +| ## Dto的好处 | | | +| ------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | +| | [2](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_2) | 1. 数据封装与传输:DTO用于在不同层之间传递数据,特别是在控制器和服务层之间。它能够将请求中的数据进行封装,从而便于传输。 | +| | [3](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_3) | 2. 解耦:通过使用DTO,可以避免直接暴露数据库模型(这里指Domain中的文件),从而降低各层之间的耦合度。 | +| | [4](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_4) | 3. 数据验证与格式化:DTO可以添加数据注释和验证属性,确保传入的数据符合预期格式和约束。这样可以在数据到达业务逻辑层之前进行初步验证。 | +| | [5](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_5) | 4. 安全性:可以避免将敏感的数据库字段直接暴露给客户端。 | +| | [6](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_6) | 5. 数据转换:以用于将复杂的数据结构转换为适合传输的简单结构,或者将客户端传入的数据转换为适合业务逻辑处理的结构。 | +| | [7](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_7) | | +| | [8](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_8) | ## 使用Dto的写法(记得引入Dto) | +| | [9](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_9) | ### Interfaces接口 | +| | [10](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_10) | ```cs | +| | [11](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_11) | **public** **interface** **IAuthorRepository** | +| | [12](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_12) | { | +| | [13](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_13) | // 获取全部作者 | +| | [14](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_14) | ICollection **GetAllAuthors**(); | +| | [15](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_15) | // 获取指定id作者 | +| | [16](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_16) | AuthorDto? **GetAuthorById**(Guid id); | +| | [17](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_17) | // 添加作者 | +| | [18](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_18) | AuthorDto **AddAuthor**(CreateAuthorDto createAuthorDto); | +| | [19](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_19) | // 修改作者 | +| | [20](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_20) | AuthorDto? **UpdateAuthor**(Guid id, UpdateAuthorDto updateAuthorDto); | +| | [21](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_21) | // 删除作者 | +| | [22](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_22) | **string** **DelAuthor**(Guid id); | +| | [23](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_23) | } | +| | [24](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_24) | ``` | +| | [25](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_25) | ### Service实现接口——增删改查的操作都写在这里 | +| | [26](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_26) | ```cs | +| | [27](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_27) | //-------------------获取全部作者------------------------------ | +| | [28](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_28) | **public** ICollection **GetAllAuthors**() | +| | [29](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_29) | { | +| | [30](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_30) | // 用于存储转换为Dto类型后的作者信息 | +| | [31](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_31) | **var** resList = **new** List(); | +| | [32](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_32) | **var** list = BookStoreDb.Instance.Authors.**ToList**(); | +| | [33](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_33) | list.**ForEach**(item => { | +| | [34](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_34) | // 将数据转换为Dto对象,并添加到resList中 | +| | [35](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_35) | /* 实例化一个对象有2种方式: | +| | [36](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_36) | 1.直接调用构造函数,形如:new AuthorDto(......) | +| | [37](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_37) | 2.直接填充其属性值,形如:new AuthorDto{......},像下面一样 | +| | [38](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_38) | */ | +| | [39](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_39) | resList.**Add**(**new** AuthorDto{ | +| | [40](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_40) | Id = item.Id, | +| | [41](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_41) | AuthorName = item.AuthorName, | +| | [42](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_42) | Gender = item.Gender | +| | [43](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_43) | }); | +| | [44](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_44) | }); | +| | [45](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_45) | **return** resList; | +| | [46](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_46) | } | +| | [47](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_47) | //-------------------获取指定id作者------------------------------ | +| | [48](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_48) | **public** AuthorDto? **GetAuthorById**(Guid id) | +| | [49](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_49) | { | +| | [50](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_50) | **var** tmp = BookStoreDb.Instance.Authors.**SingleOrDefault**(item => item.Id == id); | +| | [51](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_51) | /* | +| | [52](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_52) | 如果匹配的结果tmp不为null,则则创建一个新的 AuthorDto 对象(tmpResult),并将 tmp 中的属性值赋给它 | +| | [53](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_53) | 如果没有找到匹配的结果,则将tmpResult 设置为 null | +| | [54](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_54) | */ | +| | [55](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_55) | **var** tmpResult = tmp != **null**? **new** AuthorDto{ | +| | [56](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_56) | Id = tmp.Id, | +| | [57](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_57) | AuthorName = tmp.AuthorName, | +| | [58](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_58) | Gender = tmp.Gender | +| | [59](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_59) | }:**null**; | +| | [60](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_60) | **return** tmpResult; | +| | [61](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_61) | } | +| | [62](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_62) | //-------------------添加作者------------------------------ | +| | [63](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_63) | **public** AuthorDto **AddAuthor**(CreateAuthorDto createAuthorDto) | +| | [64](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_64) | { | +| | [65](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_65) | // 将传入的dto转换为保存到内存数据库需要的实体类型Authors | +| | [66](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_66) | **var** author = **new** Authors{ | +| | [67](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_67) | Id = Guid.**NewGuid**(), | +| | [68](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_68) | AuthorName = createAuthorDto.AuthorName, | +| | [69](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_69) | Gender = createAuthorDto.Gender, | +| | [70](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_70) | Birthday = createAuthorDto.Birthday | +| | [71](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_71) | }; | +| | [72](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_72) | // 添加操作 | +| | [73](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_73) | BookStoreDb.Instance.Authors.**Add**(author); | +| | [74](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_74) | // 将内存数据库获取的数据转换为AuthorDto的实例,以Dto类型返回 | +| | [75](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_75) | **var** authorDto = **new** AuthorDto{ | +| | [76](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_76) | Id = author.Id, | +| | [77](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_77) | AuthorName = author.AuthorName, | +| | [78](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_78) | Gender = author.Gender, | +| | [79](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_79) | }; | +| | [80](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_80) | **return** authorDto; | +| | [81](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_81) | } | +| | [82](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_82) | //-------------------修改作者------------------------------ | +| | [83](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_83) | **public** AuthorDto? **UpdateAuthor**(Guid id, UpdateAuthorDto updateAuthorDto) | +| | [84](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_84) | { | +| | [85](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_85) | // 查找指定id数据 | +| | [86](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_86) | **var** item = BookStoreDb.Instance.Authors.**SingleOrDefault**(item => item.Id == id); | +| | [87](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_87) | // 未找到就返回null | +| | [88](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_88) | **if**(item == **null**){ | +| | [89](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_89) | **return** **null**; | +| | [90](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_90) | } | +| | [91](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_91) | // 找到后就进行数据更新 | +| | [92](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_92) | item.AuthorName = updateAuthorDto.AuthorName; | +| | [93](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_93) | item.Gender = updateAuthorDto.Gender; | +| | [94](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_94) | item.Birthday = updateAuthorDto.Birthday; | +| | [95](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_95) | // 根据更新后的作者信息创建一个新的dto对象 | +| | [96](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_96) | **var** res = **new** AuthorDto{ | +| | [97](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_97) | Id = item.Id, | +| | [98](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_98) | AuthorName = item.AuthorName, | +| | [99](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_99) | Gender = item.Gender | +| | [100](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_100) | }; | +| | [101](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_101) | **return** res; | +| | [102](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_102) | } | +| | [103](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_103) | //-------------------删除作者------------------------------ | +| | [104](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_104) | //这里尝试返回string类型,成功! | +| | [105](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_105) | **public** **string** **DelAuthor**(Guid id) | +| | [106](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_106) | { | +| | [107](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_107) | **var** item = BookStoreDb.Instance.Authors.**SingleOrDefault**(item => item.Id == id); | +| | [108](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_108) | **if**(item != **null**){ | +| | [109](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_109) | **var** delAuthor = BookStoreDb.Instance.Authors.**Remove**(item); | +| | [110](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_110) | **return** "删除成功"; | +| | [111](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_111) | | +| | [112](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_112) | }**else**{ | +| | [113](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_113) | **return** "未找到删除对象"; | +| | [114](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_114) | } | +| | [115](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_115) | } | +| | [116](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_116) | ``` | +| | [117](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_117) | ### 控制器——调用函数,尽量保持纯洁性 | +| | [118](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_118) | | +| | [119](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_119) | ```cs | +| | [120](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_120) | // -----------------获取------------------------------------------ | +| | [121](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_121) | [**HttpGet**("{id?}",Name = **nameof**(Get))] | +| | [122](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_122) | **public** IActionResult **Get**(Guid id) | +| | [123](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_123) | { | +| | [124](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_124) | **var** noId = "00000000-0000-0000-0000-000000000000"; | +| | [125](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_125) | **if**(id.**ToString**()==noId){ | +| | [126](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_126) | **var** list = _authorRepsitory.**GetAllAuthors**(); | +| | [127](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_127) | **return** **Ok**(list); | +| | [128](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_128) | }**else**{ | +| | [129](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_129) | **var** item = _authorRepsitory.**GetAuthorById**(id); | +| | [130](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_130) | **return** **Ok**(item); | +| | [131](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_131) | } | +| | [132](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_132) | } | +| | [133](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_133) | // -----------------添加------------------------------------------ | +| | [134](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_134) | [HttpPost] | +| | [135](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_135) | **public** IActionResult **Post**(CreateAuthorDto createAuthorDto) | +| | [136](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_136) | { | +| | [137](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_137) | /* 1.拿到CreateAuthorDto类型的实例化数据--模型绑定 | +| | [138](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_138) | 2.将相关数据保存到数据库: | +| | [139](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_139) | 1. 转换CreateAuthorDto类型的数据为Authors类型 | +| | [140](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_140) | 2. 调用数据库上下文,将数据插入 | +| | [141](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_141) | */ | +| | [142](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_142) | **var** res = _authorRepsitory.**AddAuthor**(createAuthorDto); | +| | [143](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_143) | **if**(res == **null**){ | +| | [144](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_144) | **return** **Ok**("添加失败"); | +| | [145](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_145) | } | +| | [146](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_146) | **return** **Ok**("添加成功"); | +| | [147](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_147) | } | +| | [148](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_148) | // -----------------修改------------------------------------------ | +| | [149](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_149) | [**HttpPut**("{id}")] | +| | [150](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_150) | **public** IActionResult **Put**(Guid id,UpdateAuthorDto updateAuthorDto) | +| | [151](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_151) | { | +| | [152](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_152) | **var** res = _authorRepsitory.**UpdateAuthor**(id,updateAuthorDto); | +| | [153](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_153) | **if**(res == **null**){ | +| | [154](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_154) | **return** **Ok**("修改失败"); | +| | [155](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_155) | } | +| | [156](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_156) | **return** **Ok**("修改成功"); | +| | [157](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_157) | } | +| | [158](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_158) | // -----------------删除------------------------------------------ | +| | [159](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_159) | [**HttpDelete**("{id}")] | +| | [160](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_160) | **public** IActionResult **Del**(Guid id) | +| | [161](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_161) | { | +| | [162](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_162) | **var** res = _authorRepsitory.**DelAuthor**(id); | +| | [163](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_163) | **return** **Ok**(res); | +| | [164](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_164) | } | +| | [165](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#dbe8650b58a7a0a8fb6d412601175cc86a96961d_0_165) | ``` | \ No newline at end of file diff --git "a/\351\253\230\344\277\212\346\235\260/2024.6.4\351\205\215\347\275\256\346\216\245\345\217\243.md" "b/\351\253\230\344\277\212\346\235\260/2024.6.4\351\205\215\347\275\256\346\216\245\345\217\243.md" new file mode 100644 index 0000000..919a324 --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.6.4\351\205\215\347\275\256\346\216\245\345\217\243.md" @@ -0,0 +1,160 @@ +| ## 一、配置接口 | | | +| --------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | +| | [2](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_2) | ```js | +| | [3](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_3) | **public** **interface** IBookRepository | +| | [4](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_4) | { | +| | [5](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_5) | // 获取指定id图书 | +| | [6](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_6) | BookDto? GetBookById(Guid id); | +| | [7](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_7) | // 获取全部图书 | +| | [8](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_8) | ICollection GetAllBooks(Guid authorId); | +| | [9](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_9) | // 添加图书 | +| | [10](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_10) | BookDto AddBook(Guid authorId,AddBookDto addBookDto); | +| | [11](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_11) | // 修改图书 | +| | [12](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_12) | BookDto? UpdateBook(Guid bookId,BookDtoUpdate bookDtoUpdate); | +| | [13](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_13) | // 删除图书 | +| | [14](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_14) | string DelBook(Guid bookId); | +| | [15](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_15) | } | +| | [16](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_16) | ``` | +| | [17](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_17) | ## 二、实现接口 | +| | [18](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_18) | ```js | +| | [19](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_19) | // 添加图书 | +| | [20](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_20) | **public** BookDto AddBook(Guid authorId,AddBookDto addBookDto) | +| | [21](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_21) | { | +| | [22](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_22) | **var** book = **new** Books{ | +| | [23](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_23) | Id = Guid.NewGuid(), | +| | [24](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_24) | BooksName = addBookDto.BooksName, | +| | [25](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_25) | Publiser = addBookDto.Publiser, | +| | [26](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_26) | Price = addBookDto.Price, | +| | [27](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_27) | AuthorId = authorId | +| | [28](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_28) | }; | +| | [29](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_29) | BookStoreDb.Instance.Books.Add(book); | +| | [30](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_30) | **var** bookDto = **new** BookDto{ | +| | [31](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_31) | Id = book.Id, | +| | [32](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_32) | BooksName = book.BooksName, | +| | [33](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_33) | Publiser = book.Publiser, | +| | [34](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_34) | Price = book.Price, | +| | [35](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_35) | AuthorId = book.AuthorId | +| | [36](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_36) | }; | +| | [37](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_37) | **return** bookDto; | +| | [38](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_38) | } | +| | [39](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_39) | // 获取全部图书 | +| | [40](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_40) | **public** ICollection GetAllBooks(Guid authorId) | +| | [41](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_41) | { | +| | [42](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_42) | **var** resList = **new** List(); | +| | [43](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_43) | **var** list = BookStoreDb.Instance.Books.Where(b => b.AuthorId == authorId).ToList(); | +| | [44](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_44) | list.ForEach(item => { | +| | [45](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_45) | resList.Add(**new** BookDto{ | +| | [46](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_46) | Id = item.Id, | +| | [47](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_47) | BooksName = item.BooksName, | +| | [48](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_48) | Publiser = item.Publiser, | +| | [49](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_49) | Price = item.Price, | +| | [50](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_50) | AuthorId = item.AuthorId | +| | [51](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_51) | }); | +| | [52](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_52) | }); | +| | [53](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_53) | **return** resList; | +| | [54](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_54) | } | +| | [55](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_55) | // 获取指定id图书 | +| | [56](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_56) | **public** BookDto? GetBookById(Guid id) | +| | [57](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_57) | { | +| | [58](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_58) | **var** tmp = BookStoreDb.Instance.Books.SingleOrDefault(item=>item.Id==id); | +| | [59](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_59) | **var** tmpResult = tmp != **null**? **new** BookDto{ | +| | [60](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_60) | Id = tmp.Id, | +| | [61](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_61) | BooksName = tmp.BooksName, | +| | [62](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_62) | Publiser = tmp.Publiser, | +| | [63](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_63) | Price = tmp.Price, | +| | [64](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_64) | AuthorId = tmp.AuthorId | +| | [65](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_65) | }:**null**; | +| | [66](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_66) | **return** tmpResult; | +| | [67](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_67) | } | +| | [68](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_68) | // 修改图书 | +| | [69](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_69) | **public** BookDto? UpdateBook(Guid bookId,BookDtoUpdate bookDtoUpdate) | +| | [70](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_70) | { | +| | [71](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_71) | **var** item = BookStoreDb.Instance.Books.SingleOrDefault(b=>b.Id==bookId); | +| | [72](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_72) | **if**(item == **null**){ | +| | [73](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_73) | **return** **null**; | +| | [74](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_74) | } | +| | [75](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_75) | item.BooksName = bookDtoUpdate.BooksName; | +| | [76](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_76) | item.Publiser = bookDtoUpdate.Publiser; | +| | [77](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_77) | item.Price = bookDtoUpdate.Price; | +| | [78](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_78) | **var** res = **new** BookDto{ | +| | [79](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_79) | Id=item.Id, | +| | [80](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_80) | Publiser=item.Publiser, | +| | [81](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_81) | Price=item.Price | +| | [82](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_82) | }; | +| | [83](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_83) | **return** res; | +| | [84](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_84) | } | +| | [85](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_85) | // 删除图书 | +| | [86](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_86) | **public** string DelBook(Guid id) | +| | [87](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_87) | { | +| | [88](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_88) | **var** book = BookStoreDb.Instance.Books.SingleOrDefault(b => b.Id==id); | +| | [89](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_89) | **if**(book != **null**){ | +| | [90](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_90) | BookStoreDb.Instance.Books.Remove(book); | +| | [91](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_91) | **return** "删除成功"; | +| | [92](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_92) | } | +| | [93](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_93) | **return** "删除失败"; | +| | [94](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_94) | } | +| | [95](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_95) | ``` | +| | [96](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_96) | ## 三、控制器操作 | +| | [97](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_97) | ```js | +| | [98](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_98) | //------------------------------获取图书----------------------------- | +| | [99](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_99) | [HttpGet("{bookId?}")] | +| | [100](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_100) | **public** IActionResult Get(Guid authorId,Guid? bookId) | +| | [101](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_101) | { | +| | [102](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_102) | **var** author = AuthorRepository.GetAuthorById(authorId); | +| | [103](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_103) | **if** (author == **null**) | +| | [104](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_104) | { | +| | [105](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_105) | **return** NotFound("未找到作者"); | +| | [106](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_106) | } | +| | [107](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_107) | | +| | [108](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_108) | **if** (bookId.HasValue) | +| | [109](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_109) | { | +| | [110](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_110) | **var** book = BookRepository.GetBookById(bookId.Value); | +| | [111](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_111) | **if** (book == **null** \|\| book.AuthorId != authorId) | +| | [112](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_112) | { | +| | [113](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_113) | **return** NotFound("未找到该图书"); | +| | [114](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_114) | } | +| | [115](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_115) | **return** Ok(book); | +| | [116](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_116) | } | +| | [117](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_117) | **else** | +| | [118](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_118) | { | +| | [119](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_119) | **var** books = BookRepository.GetAllBooks(authorId); | +| | [120](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_120) | **return** Ok(books); | +| | [121](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_121) | } | +| | [122](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_122) | } | +| | [123](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_123) | //------------------------------添加图书----------------------------- | +| | [124](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_124) | [HttpPost] | +| | [125](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_125) | **public** IActionResult Post(Guid authorId,AddBookDto addBookDto) | +| | [126](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_126) | { | +| | [127](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_127) | **var** author = AuthorRepository.GetAuthorById(authorId); | +| | [128](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_128) | **if**(author == **null**){ | +| | [129](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_129) | **return** NotFound("未找到作者"); | +| | [130](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_130) | } | +| | [131](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_131) | **var** bookDto = BookRepository.AddBook(authorId, addBookDto); | +| | [132](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_132) | **return** Ok(bookDto); | +| | [133](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_133) | } | +| | [134](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_134) | //------------------------------修改图书----------------------------- | +| | [135](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_135) | [HttpPut("{bookId}")] | +| | [136](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_136) | **public** IActionResult Put(Guid authorId,Guid bookId,BookDtoUpdate bookDtoUpdate) | +| | [137](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_137) | { | +| | [138](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_138) | **var** author = AuthorRepository.GetAuthorById(authorId); | +| | [139](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_139) | **if**(author == **null**){ | +| | [140](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_140) | **return** NotFound("未找到作者"); | +| | [141](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_141) | } | +| | [142](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_142) | **var** res = BookRepository.UpdateBook(bookId,bookDtoUpdate); | +| | [143](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_143) | **if**(res == **null**){ | +| | [144](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_144) | **return** Ok("修改失败"); | +| | [145](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_145) | } | +| | [146](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_146) | **return** Ok("修改成功"); | +| | [147](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_147) | } | +| | [148](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_148) | //------------------------------删除图书----------------------------- | +| | [149](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_149) | [HttpDelete("{bookId}")] | +| | [150](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_150) | **public** IActionResult Del(Guid authorId,Guid bookId) | +| | [151](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_151) | { | +| | [152](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_152) | **var** author = AuthorRepository.GetAuthorById(authorId); | +| | [153](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_153) | **if**(author == **null**){ | +| | [154](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_154) | **return** NotFound("未找到作者"); | +| | [155](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_155) | } | +| | [156](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_156) | **var** book = BookRepository.DelBook(bookId); | +| | [157](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_157) | **return** Ok(book); | +| | [158](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_158) | } | +| | [159](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#3fb03c636b65081bceb7697a8191a711520b723f_0_159) | ``` | \ No newline at end of file diff --git "a/\351\253\230\344\277\212\346\235\260/2024.6.7EF Core\347\232\204\344\270\244\347\247\215\344\275\277\347\224\250\346\226\271\346\263\225.md" "b/\351\253\230\344\277\212\346\235\260/2024.6.7EF Core\347\232\204\344\270\244\347\247\215\344\275\277\347\224\250\346\226\271\346\263\225.md" new file mode 100644 index 0000000..1198ac8 --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.6.7EF Core\347\232\204\344\270\244\347\247\215\344\275\277\347\224\250\346\226\271\346\263\225.md" @@ -0,0 +1,97 @@ +| ## 一、EF Core的两种使用方法 | | | +| ---------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | +| | [2](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_2) | ### 1.代码优先(推荐) | +| | [3](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_3) | - 根据先创建好的实体类来创建数据库和表 | +| | [4](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_4) | - EF Core会将对实体类的修改同步到数据库中,都是对数据库手工修改将会在EF Core同步数据后丢失 | +| | [5](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_5) | - 用于同步代码到数据库的方法是迁移(Migration),就是提供以增量的方式来修改数据库和表结构,使实体类和数据库保持一致 | +| | [6](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_6) | ### 2.数据库优先 | +| | [7](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_7) | 根据先创建好的数据库生成相应的代码 | +| | [8](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_8) | | +| | [9](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_9) | ## 二、EF Core的添加步骤(代码优先) | +| | [10](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_10) | ### 1.创建数据模型 | +| | [11](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_11) | 创建数据模型类,这些类将映射到数据库中的表 | +| | [12](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_12) | | +| | [13](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_13) | ### 2. 配置数据库连接 | +| | [14](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_14) | ```c# | +| | [15](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_15) | // appsetting.json | +| | [16](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_16) | "ConnectionString":{ | +| | [17](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_17) | Mssql:"server=.;database=BookStore;uid=sa;pwd=123456;TrustServerCertificate=true;" | +| | [18](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_18) | } | +| | [19](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_19) | ``` | +| | [20](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_20) | | +| | [21](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_21) | ### 3.配置数据库上下文DbContext | +| | [22](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_22) | - DbContext是非常重要的类,代表程序与数据库之间的会话或数据上下文,能够完成查询和保存数据等操作 | +| | [23](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_23) | - 它的派生类中有若干个DbSet类型的公告属性,表示相应实体的集合,对它们的操作最后会反映到数据表 | +| | [24](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_24) | ```c# | +| | [25](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_25) | // Db/BookStoreDbContext.cs | +| | [26](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_26) | **using** **Microsoft.EntityFrameworkCore**; | +| | [27](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_27) | **namespace** **BookStore.Api.Db**; | +| | [28](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_28) | **public** **class** **BookStoreDbContext**:DbContext | +| | [29](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_29) | { | +| | [30](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_30) | **public** DbSet Authors {**get**;**set**;} | +| | [31](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_31) | **public** DbSet Books {**get**;**set**;} | +| | [32](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_32) | // 构造函数接受一个 DbContextOptions 类型的参数 | +| | [33](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_33) | // 将该参数传递给基类的构造函数 base(options) | +| | [34](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_34) | // 这个构造函数用于配置数据库上下文的选项,例如数据库提供程序、连接字符串等 | +| | [35](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_35) | **public** **BookStoreDbContext**(DbContextOptions options):**base**(options) | +| | [36](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_36) | { | +| | [37](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_37) | | +| | [38](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_38) | } | +| | [39](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_39) | } | +| | [40](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_40) | ``` | +| | [41](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_41) | ***\*注意点:\**** | +| | [42](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_42) | - 提供正确的连接字符串 | +| | [43](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_43) | - 必须构造函数选项配置 | +| | [44](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_44) | | +| | [45](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_45) | ### 4.注入服务,引入配置文件,实现连接数据库 | +| | [46](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_46) | ```c# | +| | [47](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_47) | // Startup.cs | +| | [48](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_48) | servuces.AddDbContext(config => | +| | [49](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_49) | config.**UseSqlServer**(Configuration.**GetConnectionString**("Mssql")); | +| | [50](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_50) | // 也可以直接在GetConnectionString()里写连接字符串内容 | +| | [51](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_51) | ) | +| | [52](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_52) | ``` | +| | [53](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_53) | ***\*注意:\**** | +| | [54](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_54) | 这里UseSqlServer只有在Microsoft.EntityFrameworkcore.SqlServer包安装后才可以使用 | +| | [55](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_55) | | +| | [56](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_56) | ### 5.生成迁移与创建数据库 | +| | [57](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_57) | ```js | +| | [58](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_58) | // 安装dotnet-ef工具 | +| | [59](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_59) | dotnet tool install -g dotnet-ef | +| | [60](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_60) | // 生成迁移 | +| | [61](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_61) | dotnet ef migrations add <迁移名称> | +| | [62](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_62) | // 如果迁移成功就会出现Migrations文件夹 | +| | [63](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_63) | | +| | [64](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_64) | // 创建数据库(同步) | +| | [65](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_65) | | +| | [66](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_66) | dotnet ef database update | +| | [67](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_67) | // 创建成功就会在数据库出现表,其中会自带一个名为_EFMigrationsHistory的表,这是存储迁移的历史记录,删除就会导致程序运行失败 | +| | [68](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_68) | ``` | +| | [69](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_69) | ***\*注意:\**** | +| | [70](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_70) | - 迁移名称最好是英文且首字母大写 | +| | [71](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_71) | - 使用迁移前需要安装Microsoft.EntityFrameworkcore | +| | [72](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_72) | - 需要全局安装dotnet-ef工具 | +| | [73](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_73) | - 代码不能有错误,否则迁移中会报错 | +| | [74](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_74) | | +| | [75](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_75) | ### 6.添加种子数据 | +| | [76](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_76) | #### 为种子数据建模 | +| | [77](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_77) | ```c# | +| | [78](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_78) | // OnModelCreating.cs | +| | [79](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_79) | **protected** **override** **void** **OnModelCreating**(ModelBuilder modelBuider) | +| | [80](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_80) | { | +| | [81](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_81) | **base**.**OnModelCreating**(modelBuider); | +| | [82](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_82) | modelBuilder.Entity().**HasData**(**new** Author{ | +| | [83](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_83) | Id = **new** Guid, | +| | [84](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_84) | AuthorName = "小米" | +| | [85](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_85) | }, | +| | [86](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_86) | **new** Author{ | +| | [87](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_87) | ...... | +| | [88](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_88) | } | +| | [89](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_89) | ); | +| | [90](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_90) | modelBuilder.Entity().**HasData**(**new** Book{......}); | +| | [91](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_91) | } | +| | [92](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_92) | // HasData方法可添加一个或多个相同的实体类型 | +| | [93](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_93) | ``` | +| | [94](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_94) | ***\*注意:\**** | +| | [95](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_95) | 1. 通常情况下数据库会自动生成主键值,但在种子数据中,您仍然需要为主键字段指定值 | +| | [96](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#2ec3ccea30b697ca6387d42757224eb10bd95fc5_0_96) | 2. 如果以任何方式更改主键,将删除之前设定种子的数据 | \ No newline at end of file diff --git "a/\351\253\230\344\277\212\346\235\260/2024.6.EF Core\347\232\204\347\256\200\344\273\213.md" "b/\351\253\230\344\277\212\346\235\260/2024.6.EF Core\347\232\204\347\256\200\344\273\213.md" new file mode 100644 index 0000000..a42071d --- /dev/null +++ "b/\351\253\230\344\277\212\346\235\260/2024.6.EF Core\347\232\204\347\256\200\344\273\213.md" @@ -0,0 +1,66 @@ +| ## 一、EF Core的简介 | | | +| -------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | +| | [2](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_2) | | +| | [3](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_3) | ### 1.概念 | +| | [4](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_4) | | +| | [5](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_5) | - EF Core(Entity Framework Core)是基于.NET Core的轻量级ORM框架 | +| | [6](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_6) | - ORM能够处理数据库与高级编程语言中对象之间的映射关系 | +| | [7](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_7) | | +| | [8](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_8) | ### 2. .NET对象与关系型数据库的对应关系 | +| | [9](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_9) | | +| | [10](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_10) | \| .NET对象 \| 关系型数据库 \| | +| | [11](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_11) | \| :--------------: \| :----------: \| | +| | [12](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_12) | \| 类 \| 表 \| | +| | [13](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_13) | \| 类的属性或字段 \| 表中的列 \| | +| | [14](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_14) | \| 集合中的元素 \| 表中的行 \| | +| | [15](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_15) | \| 对于其他类的引用 \| 外键 \| | +| | [16](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_16) | | +| | [17](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_17) | ### 3.支持LINQ(集成语言查询) | +| | [18](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_18) | | +| | [19](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_19) | #### 1.查询所有 | +| | [20](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_20) | | +| | [21](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_21) | ```cs | +| | [22](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_22) | **var** result = **from** item **in** collection | +| | [23](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_23) | **select** item; | +| | [24](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_24) | ``` | +| | [25](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_25) | | +| | [26](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_26) | #### 2.过滤 | +| | [27](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_27) | | +| | [28](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_28) | ```cs | +| | [29](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_29) | **var** result = **from** item **in** collection | +| | [30](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_30) | **where** price > **10** | +| | [31](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_31) | **select** item; | +| | [32](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_32) | ``` | +| | [33](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_33) | | +| | [34](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_34) | #### 3.排序 | +| | [35](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_35) | | +| | [36](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_36) | ```cs | +| | [37](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_37) | **var** result = **from** item **in** collection | +| | [38](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_38) | **orderby** item.Property **ascending**/**descending** | +| | [39](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_39) | **select** item; | +| | [40](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_40) | //ascending(升序,默认);descending(降序)关键字 | +| | [41](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_41) | ``` | +| | [42](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_42) | | +| | [43](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_43) | #### 4.连接查询 | +| | [44](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_44) | | +| | [45](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_45) | ```cs | +| | [46](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_46) | **var** query = **from** person **in** people | +| | [47](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_47) | **join** address **in** addresses **on** person.AddressId **equals** address.Id | +| | [48](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_48) | **select** **new** { person.Name, address.City }; | +| | [49](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_49) | | +| | [50](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_50) | ``` | +| | [51](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_51) | | +| | [52](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_52) | ## 二、在项目中添加EF Core | +| | [53](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_53) | | +| | [54](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_54) | ### 1.安装包 | +| | [55](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_55) | | +| | [56](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_56) | ```js | +| | [57](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_57) | dotnet add **package** Microsoft.EntityFrameworkCore | +| | [58](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_58) | ``` | +| | [59](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_59) | | +| | [60](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_60) | ### 2.安装提供程序 | +| | [61](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_61) | | +| | [62](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_62) | ```js | +| | [63](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_63) | //这里为SQL Server数据库为例 | +| | [64](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_64) | dotnet add **package** Microsoft.EntityFrameworkCore.SqlServer | +| | [65](https://gitee.com/level-22-net-class/webapi-class-notes/pulls/196/files#4ddd42032c75bac7825e20f3ad6d696a2ae29ca6_0_65) | ``` | \ No newline at end of file -- Gitee