diff --git "a/\351\202\261\346\245\267\346\235\255/20240603 - \345\215\225\347\272\247\350\267\257\347\224\261\347\232\204\346\237\245\350\257\242.md" "b/\351\202\261\346\245\267\346\235\255/20240603 - \345\215\225\347\272\247\350\267\257\347\224\261\347\232\204\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..a185f0edd1b16eec45bfb652c6092037139b7522 --- /dev/null +++ "b/\351\202\261\346\245\267\346\235\255/20240603 - \345\215\225\347\272\247\350\267\257\347\224\261\347\232\204\346\237\245\350\257\242.md" @@ -0,0 +1,102 @@ +# 单级路由的查询· +## 接口 +```C# + using BookStore.Api.Dto; + +namespace BookStore.Api.Interface; + +public interface IAuthorRepository +{ + // 通过Id获取指定作者的方法 + AuthorDto? GetAuthorById(Guid id); + + // 获取所有作者的方法 + // 函数三要素:函数名称、函数参数、函数返回值 + ICollection GetAllAuthors(); + + + AuthorDto Insert(AuthorCreateDto authorCreateDto); + +} + ``` +## 实现 +```C# + public AuthorDto Insert(AuthorCreateDto authorCreateDto) + { + // 将传入的dto转换为保存到数据库需要的实体类型 + var author = new Authors + { + Id = Guid.NewGuid(), + AuthorName = authorCreateDto.AuthorName, + Gender = authorCreateDto.Gender, + Birthday = authorCreateDto.Birthday, + }; + // 插入数据库 + BookStoreDb.Instance.Authors.Add(author); + // 将内存数据库获取的数据转换为AuthorDto的实例 + var authorDto = new AuthorDto { Id = author.Id, AuthorName = author.AuthorName, Gender = author.Gender }; + return authorDto; + } + + public ICollection GetAllAuthors() + { + // 这个实现应该从持久化的数据源中获得:数据库、文件、各种异构数据、从各种api中获取的数据 + var list = BookStoreDb.Instance.Authors.ToList(); + var resultList = new List();// let resultList=[] + list.ForEach(item => + { + // 实例化一个对象这里有2种方式,一个是直接调用构造函数,形如:new AuthorDto() + // 另一个是直接填充其属性,形如:new AuthorDto{} + var tmp = new AuthorDto { Id = item.Id, AuthorName = item.AuthorName, Gender = item.Gender }; + resultList.Add(tmp); + }); + return resultList; + } + + public AuthorDto? GetAuthorById(Guid id) + { + var tmp = BookStoreDb.Instance.Authors.SingleOrDefault(item => item.Id == id); + var tmpResult = tmp != null ? new AuthorDto { Id = tmp.Id, AuthorName = tmp.AuthorName, Gender = tmp.Gender } : null; + // dynamic xResult=null; + // if(tmp!=null){ + // xResult=new AuthorDto { Id = tmp.Id, AuthorName = tmp.AuthorName, Gender = tmp.Gender }; + // } + return tmpResult; + } + ``` +## action +```C# + [HttpGet("{id?}")] + public IActionResult Get(Guid id) + { + if (id.ToString() == "00000000-0000-0000-0000-000000000000") + { + return Ok(_authorRepository.GetAllAuthors()); + } + else + { + var item = _authorRepository.GetAuthorById(id); + return Ok(item); + } + } + + /// + /// 新增作者 + /// + /// 接受的参数 + /// + [HttpPost] + public IActionResult Post(AuthorCreateDto authorCreateDto) + { + /* + 1.拿到AuthorCreateDto类型的实例化数据-模型绑定 + 2.将相关数据保存到数据库 + 1.转换AuthorCreateDto类型的数据为Authors + 2.调用数据库上下文,将数据插入 + + */ + var result = _authorRepository.Insert(authorCreateDto); + return Ok(result); + } + +``` \ No newline at end of file diff --git "a/\351\202\261\346\245\267\346\235\255/20240604 - \345\215\225\347\272\247\350\267\257\347\224\261\347\232\204\344\277\256\345\210\240.md" "b/\351\202\261\346\245\267\346\235\255/20240604 - \345\215\225\347\272\247\350\267\257\347\224\261\347\232\204\344\277\256\345\210\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..0791e32b6e19575e68314d27910647aad1b0565a --- /dev/null +++ "b/\351\202\261\346\245\267\346\235\255/20240604 - \345\215\225\347\272\247\350\267\257\347\224\261\347\232\204\344\277\256\345\210\240.md" @@ -0,0 +1,78 @@ +# 单级路由的修删 +## 接口 +```C# + using BookStore.Api.Dto; + +namespace BookStore.Api.Interface; + +public interface IAuthorRepository +{ + // 通过Id获取指定作者的方法 + + // 获取所有作者的方法 + // 函数三要素:函数名称、函数参数、函数返回值 + + AuthorDto? Update(Guid authorId,AuthorUpdateDto authorUpdateDto); + AuthorDto? Delete(Guid authorId); + + +} + ``` +## 实现 +```C# + public AuthorDto? Update(Guid authorId, AuthorUpdateDto authorUpdateDto) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + + // 修改值 + author.AuthorName = authorUpdateDto.AuthorName; + author.Gender = authorUpdateDto.Gender; + author.Birthday = authorUpdateDto.Birthday; + + var result = new AuthorDto { Id = author.Id, AuthorName = author.AuthorName, Gender = author.Gender }; + return result; + + } + + public AuthorDto? Delete(Guid authorId) + { + // 先找出满足id的元素,然后再移除对应元素 + var tmp = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + // BookStoreDb.Instance.Authors.Remove(tmp); + // var tmpResult = tmp != null ? new AuthorDto { Id = tmp.Id, AuthorName = tmp.AuthorName, Gender = tmp.Gender }:null; + if (tmp != null) + { + BookStoreDb.Instance.Authors.Remove(tmp); + var dto = new AuthorDto { Id = tmp.Id, AuthorName = tmp.AuthorName, Gender = tmp.Gender }; + return dto; + } + else + { + return null; + } + } +``` +## action +```C# + + [HttpPut("{id}")] + public IActionResult Put(Guid id, AuthorUpdateDto authorUpdateDto) + { + var result = _authorRepository.Update(id, authorUpdateDto); + return Ok(result); + } + + [HttpDelete("{id}")] + public IActionResult Del(Guid id) + { + // return Ok(id); + var author = _authorRepository.Delete(id); + + + return Ok(author); + } +``` \ No newline at end of file diff --git "a/\351\202\261\346\245\267\346\235\255/20240606 - \345\244\232\347\272\247\350\267\257\347\224\261crud.md" "b/\351\202\261\346\245\267\346\235\255/20240606 - \345\244\232\347\272\247\350\267\257\347\224\261crud.md" new file mode 100644 index 0000000000000000000000000000000000000000..b60e54135df5a9975b7a5550a3c7dd9e17f86e59 --- /dev/null +++ "b/\351\202\261\346\245\267\346\235\255/20240606 - \345\244\232\347\272\247\350\267\257\347\224\261crud.md" @@ -0,0 +1,190 @@ +# 多级路由的crud +## 接口 +```C# + using BookStore.Api.Dto; + +namespace BookStore.Api.Interface; + +public interface IBookRepository +{ + // 通过Id获取指定作者的方法 + BookDto? GetBookById(Guid authorId,Guid bookId); + + // 获取所有作者的方法 + // 函数三要素:函数名称、函数参数、函数返回值 + ICollection GetAllBooks(Guid authorId); + + + BookDto? Insert(Guid authorId,BookCreateDto bookCreateDto); + BookDto? Update(Guid authorId,Guid BookId, BookUpdateDto bookUpdateDto); + BookDto? Delete(Guid authorId,Guid BookId); +} +``` +## 实现 +```C# + public BookDto? Delete(Guid authorId, Guid bookId) + { + /* + 1. 确认要操作的作者是否存在 + 存在则继续 + 不存在则返回null + 2. 在内存数据库查找对应的图书id + 存在则删除对应图书 + 不存在则返回null + */ + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.Id == bookId); + if (book == null) + { + return null; + } + BookStoreDb.Instance.Books.Remove(book); + var result = new BookDto { Id = bookId, AuthorId = authorId, BookName = book.BookName }; + return result; + } + + public ICollection GetAllBooks(Guid authorId) + { + /* + 较好的做法,是确认对应authorId下有没有对应的作者, + 并且作者的状态是否是正确的(没有被删除,没有被禁用,没有被限制功能) + + 如果业务不允许,则返回作者状态不正确的结果 + */ + var list = BookStoreDb.Instance.Books.Where(item => item.AuthorId == authorId) + .Select(item => new BookDto { Id = item.Id, AuthorId = item.AuthorId, BookName = item.BookName }).ToList(); + return list; + } + + public BookDto? GetBookById(Guid authorId, Guid bookId) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.AuthorId == authorId && item.Id == bookId); + + if (book == null) + { + return null; + } + return new BookDto { Id = book.Id, BookName = book.BookName, AuthorId = book.AuthorId }; + } + + public BookDto? Insert(Guid authorId, BookCreateDto bookCreateDto) + { + /* + 1.确认传入的authorId存在对应的记录 + 如果存在,继续后面的流程;否则返回null + 2.将输入的数据进行整理转换,插入到数据中 + 1. 将BookCreateDto类型的数据转换成Books类型的数据 + 2. 将转换到books数据插入数据库表 + + 为了简单,目前从未对传入数据进行任何验证,原因就是为了让整个应用尽量简单,后续一定要进行数据验证 + 1. 简单验证 数据类型,是否为空,字符长度,是否一定格式等等 + 2. 复杂验证或者叫业务验证 + + */ + + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + var tmp = new Books + { + Id = Guid.NewGuid(), + BookName = bookCreateDto.BookName, + Publisher = bookCreateDto.Publisher, + Price = bookCreateDto.Price, + AuthorId = authorId + }; + + BookStoreDb.Instance.Books.Add(tmp); + + var result = new BookDto { Id = tmp.Id, BookName = tmp.BookName, AuthorId = tmp.AuthorId, Author = author }; + + return result; + } + + public BookDto? Update(Guid authorId, Guid BookId, BookUpdateDto bookUpdateDto) + { + /* + 1. 要修改的指定的作者是不是在,如果不在则返回null,如果在则继续 + 2. 要修改的指定的图书book是不是存在,如果不存在则返回null,如果存在则继续 + 3. 完成book数据的更改和保存 + 将传入的bookUpdateDto数据赋值给book,就可以了 + + */ + + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.Id == BookId); + if (book == null) + { + return null; + } + + book.BookName = bookUpdateDto.BookName; + book.Publisher = bookUpdateDto.Publisher; + book.Price = bookUpdateDto.Price; + book.BookName = bookUpdateDto.BookName; + + var result = new BookDto { Id = book.Id, BookName = book.BookName, AuthorId = book.AuthorId }; + return result; + } + ``` +## action +```C# + + private readonly IBookRepository _bookRepository; + + public BooksController(IBookRepository bookRepository) + { + _bookRepository = bookRepository; + } + + [HttpGet("{bookId?}")] + public IActionResult Get(Guid authorId, Guid bookId) + { + if (bookId.ToString() == "00000000-0000-0000-0000-000000000000") + { + var list = _bookRepository.GetAllBooks(authorId); + return Ok(list); + } + + var result = _bookRepository.GetBookById(authorId, bookId); + + return Ok(result); + } + + [HttpPost] + public IActionResult Post(Guid authorId, BookCreateDto bookCreateDto) + { + var result = _bookRepository.Insert(authorId, bookCreateDto); + return Ok(result); + } + + [HttpPut("{bookId}")] + public IActionResult Put(Guid authorId, Guid bookId, BookUpdateDto bookUpdateDto) + { + var result = _bookRepository.Update(authorId, bookId, bookUpdateDto); + return Ok(result); + } + + [HttpDelete("{bookId}")] + public IActionResult Delete(Guid authorId, Guid bookId) + { + var result = _bookRepository.Delete(authorId, bookId); + return Ok(result); + } +``` \ No newline at end of file diff --git "a/\351\202\261\346\245\267\346\235\255/20240607 - EFcore.md" "b/\351\202\261\346\245\267\346\235\255/20240607 - EFcore.md" new file mode 100644 index 0000000000000000000000000000000000000000..849e54e5e4da1aae7f0ba7a7d5c6f02e002ad5c9 --- /dev/null +++ "b/\351\202\261\346\245\267\346\235\255/20240607 - EFcore.md" @@ -0,0 +1,70 @@ +# efcore 连接sql server数据库 +在.NET Core中使用Entity Framework Core (EF Core) 连接SQL Server数据库,你需要完成以下几个步骤: + +1. 安装必要的NuGet包。 +2. 创建数据库上下文类。 +3. 配置数据库连接字符串。 +4. 使用数据库上下文进行数据库操作。 + +以下是具体步骤和示例代码: + +### 1\. 安装NuGet包 + +首先,你需要在你的.NET Core项目中安装以下NuGet包: + +* `Microsoft.EntityFrameworkCore` +* `Microsoft.EntityFrameworkCore.SqlServer` + +你可以在项目目录下打开命令行窗口,使用以下命令安装: + + dotnet add package Microsoft.EntityFrameworkCore + dotnet add package Microsoft.EntityFrameworkCore.SqlServer + + +### 2\. 创建数据库上下文类 + +接下来,你需要创建一个继承自`DbContext`的类,这个类将作为你的数据库上下文。 + + using Microsoft.EntityFrameworkCore; + + public class ApplicationDbContext : DbContext + { + public DbSet YourEntities { get; set; } // 代表数据库中的表 + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // 配置数据库连接字符串 + optionsBuilder.UseSqlServer(@"Server=你的服务器地址;Database=数据库名;Integrated Security=True;"); // 根据实际情况修改 + } + } + + +### 3\. 配置数据库连接字符串 + +在上面的`OnConfiguring`方法中,你需要替换为你的SQL Server实例的实际连接字符串。如果使用Windows身份验证,可以使用`Integrated Security=True;`。如果使用SQL Server身份验证,则需要提供用户名和密码。 + +### 4\. 使用数据库上下文进行数据库操作 + +以下是一个示例,演示如何使用`ApplicationDbContext`来添加一个实体到数据库: + + using (var context = new ApplicationDbContext()) + { + var entity = new YourEntity + { + // 设置实体的属性 + }; + + context.YourEntities.Add(entity); + context.SaveChanges(); + } + + +这里`YourEntity`是你定义的模型类,它代表数据库中的一个表。 + +### 注意事项 + +* 确保你的项目已经 targeting 正确的.NET Core版本。 +* 如果你的项目是ASP.NET Core Web应用程序,你可能会在`Startup.cs`文件中配置数据库上下文和连接字符串,而不是在`DbContext`的`OnConfiguring`方法中。 +* 在生产环境中,数据库连接字符串通常不会硬编码在代码中,而是存储在配置文件(如`appsettings.json`)或环境变量中。 + +以上就是.NET Core中EF Core连接SQL Server数据库的基本步骤和示例代码。 \ No newline at end of file