From ff1e680a5ce1c6f31ff4ca1b71a4f45fc9f15c5d Mon Sep 17 00:00:00 2001 From: zhao <123> Date: Mon, 10 Jun 2024 15:14:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=BA=94=E5=91=A8=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\343\200\201\347\274\226\350\276\221.md" | 194 ++++++++++++++++++ ...36\347\216\260\345\210\240\351\231\244.md" | 60 ++++++ .../0606\345\256\236\347\216\260crud.md" | 174 ++++++++++++++++ .../0607EF Core.md" | 149 ++++++++++++++ 4 files changed, 577 insertions(+) create mode 100644 "\350\256\270\345\205\260\350\216\271/0603\345\256\236\347\216\260\346\267\273\345\212\240\343\200\201\347\274\226\350\276\221.md" create mode 100644 "\350\256\270\345\205\260\350\216\271/0604\345\256\236\347\216\260\345\210\240\351\231\244.md" create mode 100644 "\350\256\270\345\205\260\350\216\271/0606\345\256\236\347\216\260crud.md" create mode 100644 "\350\256\270\345\205\260\350\216\271/0607EF Core.md" diff --git "a/\350\256\270\345\205\260\350\216\271/0603\345\256\236\347\216\260\346\267\273\345\212\240\343\200\201\347\274\226\350\276\221.md" "b/\350\256\270\345\205\260\350\216\271/0603\345\256\236\347\216\260\346\267\273\345\212\240\343\200\201\347\274\226\350\276\221.md" new file mode 100644 index 0000000..db4deb3 --- /dev/null +++ "b/\350\256\270\345\205\260\350\216\271/0603\345\256\236\347\216\260\346\267\273\345\212\240\343\200\201\347\274\226\350\276\221.md" @@ -0,0 +1,194 @@ +### 增加、编辑 + +```cs +/* AuthorController.cs*/ + //获取所有 + [HttpGet("{id?}")] + public IActionResult Get(Guid id) + { + //用我们注入的接口,使用其中我们定义的函数方法去实现功能 + var list = _authorRepository.GetAllAuthors(id); + return Ok(list); + } + //保存 + [HttpPost] + public ActionResult Post(AuthorCreateDto AuthorCreateDto) + { + //用我们注入的接口,使用其中我们定义的函数方法去实现功能 + var list = _authorRepository.Create(AuthorCreateDto); + return Ok(list); + } + //修改 + [HttpPut("{id}")] + public ActionResult Put(Guid id, AuthorEditDto authorEditDto) + { + //用我们注入的接口,使用其中我们定义的函数方法去实现功能 + var list = _authorRepository.Edit(id, authorEditDto); + return Ok(list); + } +``` +```cs +/*AuthorStoreDb.cs */ +//初始化一个AuthorStoreDb对象 + public static AuthorStoreDb Index { get; set; } = new AuthorStoreDb(); + //初始化一个ICollection集合类型的对象,将它new成一个list的对象(Authors是你创建的Domain中的实例化对象) + public ICollection Authors { get; set; } = new List(); + public ICollection Books { get; set; } = new List(); + //给内存型的数据库添加数据,List集合使用Add添加 + public AuthorStoreDb() + { + Authors.Add(new Authors + { + Id = Guid.NewGuid(), + AuthorName = "aaaa", + Gender = 1 + }); + Authors.Add(new Authors + { + Id = Guid.NewGuid(), + AuthorName = "bbbb", + Gender = 0 + }); + Authors.Add(new Authors + { + Id = Guid.NewGuid(), + AuthorName = "cccc", + Gender = 1 + }); + Books.Add(new Books + { + Id = Guid.NewGuid(), + BookName = "aaa", + AuthorId = Guid.NewGuid() + }); + Books.Add(new Books + { + Id = Guid.NewGuid(), + BookName = "bbb", + AuthorId = Guid.NewGuid() + }); + Books.Add(new Books + { + Id = Guid.NewGuid(), + BookName = "cccc", + AuthorId = Guid.NewGuid() + }); + + } +``` +```cs +/*Startup.cs */ + +public void ConfigureServices(IServiceCollection service) + { + //注册控制器 + service.AddControllers(); + // 将仓储服务注册到容器 + service.AddScoped(); + } +``` + +```cs +/*IAuthorRepository.cs */ + +using Admin8.Domain; +using Admin8.Dto; + +namespace Admin8.Interface; + +public interface IAuthorRepository +{ + //定义接口中的函数,返回类型,形参 + //定义获取的方法 + ICollection? GetAllAuthors(Guid id); + //定义增加的方法 + AuthorDto Create(AuthorCreateDto authorCreateDto); + //定义编辑的方法 + AuthorDto? Edit(Guid id, AuthorEditDto authorEditDto); + //定义删除的方法 + AuthorDto? Delete(Guid id); +} +``` + +```cs +/*AuthorRepository.cs*/ + +//获取全部或者指定Guid的作者(AuthorDto中我的是将Password隐藏了的,如果不用的话,考试时可以直接改为Author) + public ICollection? GetAllAuthors(Guid guid) + { + //判断传进来的Id是否存在,不存在的Guid默认值为00000000-0000-0000-0000-000000000000 + if (guid.ToString() == "00000000-0000-0000-0000-000000000000") + { + //在内存型数据库中获取到所有的数据 + var list = AuthorStoreDb.Index.Authors.ToList(); + //创建一个List集合保存需要返回的数据(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下省略) + var result = new List(); + list.ForEach(item => + { + var temp = new AuthorDto { Id = item.Id, AuthorName = item.AuthorName, Gender = item.Gender }; + result.Add(temp); + }); + //将数据返回(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下返回改为list) + return result; + } + else + { + //在内存型数据库中获取到指定Guid的数据 + var list = AuthorStoreDb.Index.Authors.FirstOrDefault(item => item.Id == guid); + //没有则返回空,并结束函数 + if (list == null) + { + return null; + } + //创建一个List集合保存需要返回的数据(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下省略) + var result = new List(); + var obj = new AuthorDto { Id = list.Id, AuthorName = list.AuthorName, Gender = list.Gender }; + result.Add(obj); + //将数据返回(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下返回改为list) + return result; + } + + } + //添加作者信息 + public AuthorDto Create(AuthorCreateDto authorCreateDto) + { + //创建一个新的Authors,其中将上面传进来的authorCreateDto数据保存进去 + var obj = new Authors + { + //Guid.NewGuid()这是让他自己创建一个新的Guid + Id = Guid.NewGuid(), + AuthorName = authorCreateDto.AuthorName, + Gender = authorCreateDto.Gender, + Password = authorCreateDto.Password + }; + //将创建的数据保存进内存型数据库(List用Add添加),AuthorStoreDb中看一下结构,逐级.下去的 + AuthorStoreDb.Index.Authors.Add(obj); + var list = new AuthorDto { Id = obj.Id, AuthorName = obj.AuthorName, Gender = obj.Gender }; + //将数据返回(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下返回改为obj) + return list; + } + //修改 + public AuthorDto? Edit(Guid guid, AuthorEditDto authorEditDto) + { + //先在内存型数据库中找到是否有这个guid + var list = AuthorStoreDb.Index.Authors.FirstOrDefault(item => item.Id == guid); + if (list == null) + { + return null; + } + //不为空就不会执行上面的return,继续下面的内容 + //给找到的去数据赋值,authorEditDto是传进来修改的内容 + list.AuthorName = authorEditDto.AuthorName; + list.Gender = authorEditDto.Gender; + list.Password = authorEditDto.Password; + //创建一个AuthrDto保存需要返回的数据(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下省略) + var result = new AuthorDto + { + Id = guid, + AuthorName = list.AuthorName, + Gender = list.Gender + }; + //将数据返回(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下返回改为list) + return result; + } +``` \ No newline at end of file diff --git "a/\350\256\270\345\205\260\350\216\271/0604\345\256\236\347\216\260\345\210\240\351\231\244.md" "b/\350\256\270\345\205\260\350\216\271/0604\345\256\236\347\216\260\345\210\240\351\231\244.md" new file mode 100644 index 0000000..819645b --- /dev/null +++ "b/\350\256\270\345\205\260\350\216\271/0604\345\256\236\347\216\260\345\210\240\351\231\244.md" @@ -0,0 +1,60 @@ +### 删除 + +```cs +/* AuthorController.cs */ + //删除 + [HttpDelete("{id}")] + public IActionResult Del(Guid id) + { + //用我们注入的接口,使用其中我们定义的函数方法去实现功能 + var list = _authorRepository.Delete(id); + return Ok(list); + } +``` + +```cs +/* IAuthorRepository.cs */ +using Admin8.Domain; +using Admin8.Dto; + +namespace Admin8.Interface; + +public interface IAuthorRepository +{ + //定义接口中的函数,返回类型,形参 + //定义获取的方法 + ICollection? GetAllAuthors(Guid id); + //定义增加的方法 + AuthorDto Create(AuthorCreateDto authorCreateDto); + //定义编辑的方法 + AuthorDto? Edit(Guid id, AuthorEditDto authorEditDto); + //定义删除的方法 + AuthorDto? Delete(Guid id); +} +``` +```cs +/* AuthorRepository.cs */ + + //删除 + public AuthorDto? Delete(Guid id) + { + //先在内存型数据库中找到是否有这个guid + var obj = AuthorStoreDb.Index.Authors.FirstOrDefault(item => item.Id == id); + if (obj == null) + { + return null; + } + //不为空就不会执行上面的return,继续下面的内容 + //先在内存型数据库中使用Remove去删除这个obj内容(不是还用item什么的,直接把这个找到的对象放进去) + AuthorStoreDb.Index.Authors.Remove(obj); + //创建一个AuthrDto保存需要返回的数据(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下省略) + var result = new AuthorDto + { + Id = obj.Id, + AuthorName = obj.AuthorName, + Gender = obj.Gender + }; + //将数据返回(我这里返回的数据中将Password隐藏了,如果不需要隐藏,以下返回改为要删除的对象obj) + return result; + } +``` \ No newline at end of file diff --git "a/\350\256\270\345\205\260\350\216\271/0606\345\256\236\347\216\260crud.md" "b/\350\256\270\345\205\260\350\216\271/0606\345\256\236\347\216\260crud.md" new file mode 100644 index 0000000..6901a89 --- /dev/null +++ "b/\350\256\270\345\205\260\350\216\271/0606\345\256\236\347\216\260crud.md" @@ -0,0 +1,174 @@ +### 实现CRUD + +```cs +/* BookController.cs */ +using BookStore.Api.Dto; +using BookStore.Api.Interface; +using Microsoft.AspNetCore.Mvc; + +namespace BookStore.Api.Controller; + +[ApiController] +[Route("api/authors/{authorId}/[controller]/")] +public class BooksController : ControllerBase +{ + + 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); + } +} +``` + +```cs +/* BookRepository.cs */ +using BookStore.Api.Db; +using BookStore.Api.Domain; +using BookStore.Api.Dto; +using BookStore.Api.Interface; + +namespace BookStore.Api.Services; + +public class BookRepository : IBookRepository +{ + public BookDto? Delete(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.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) + { + 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) + { + 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) + { + 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; + } +} +``` + +```cs +/*Startup.cs */ +public void ConfigureServices(IServiceCollection services) + { + // 将swagger端点服务和界面服务注册到容器 + services.AddEndpointsApiExplorer(); + services.AddSwaggerGen(); + + // 将控制器服务注册到容器 + services.AddControllers(); + + // 将仓储服务注册到容器 + services.AddScoped(); + services.AddScoped(); + } +``` \ No newline at end of file diff --git "a/\350\256\270\345\205\260\350\216\271/0607EF Core.md" "b/\350\256\270\345\205\260\350\216\271/0607EF Core.md" new file mode 100644 index 0000000..c763690 --- /dev/null +++ "b/\350\256\270\345\205\260\350\216\271/0607EF Core.md" @@ -0,0 +1,149 @@ +Entity Framework Core (EF Core) +一、入门基础 +------ + +### 1.1 简介 + +Entity Framework Core(EF Core)是微软推出的一种轻量级、可扩展、开源且跨平台的对象关系映射(ORM)框架。它允许.NET开发人员使用.NET对象来操作数据库,而无需编写大量数据访问代码。 + +### 1.2 环境搭建 + +在开始使用EF Core之前,需要确保安装了.NET Core SDK和Visual Studio或者Visual Studio Code。 + +二、使用方法 +------ + +### 2.1 创建项目 + +以.NET Core控制台项目为例,创建一个新的项目。 + + // 使用.NET CLI命令创建一个新的.NET Core控制台项目 + dotnet new console -n EFCoreDemo + + +### 2.2 安装EF Core NuGet包 + +在创建的项目中安装EF Core的NuGet包。 + + // 使用.NET CLI命令安装EF Core SQL Server包 + dotnet add package Microsoft.EntityFrameworkCore.SqlServer + + +### 2.3 创建实体类 +```cs +创建一个实体类,用于表示数据库中的表。 + + // 实体类 + public class Student + { + // 主键 + public int Id { get; set; } + + // 学生姓名 + public string Name { get; set; } + } +``` + +### 2.4 创建数据库上下文 + +创建一个继承自`DbContext`的数据库上下文类,用于管理数据库连接和实体类。 +```cs + // 数据库上下文类 + public class MyDbContext : DbContext + { + // 实体集 + public DbSet Students { get; set; } + + // 配置数据库连接 + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // 使用SQLite数据库文件 + optionsBuilder.UseSqlite("Data Source=EFCoreDemo.db"); + } + + // 配置模型 + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + // 可以在这里配置实体类的映射关系 + } + } + ``` + +### 2.5 数据库迁移 + +使用迁移命令来创建和更新数据库。 + + // 使用.NET CLI命令添加迁移 + dotnet ef migrations add Init + + // 使用.NET CLI命令更新数据库 + dotnet ef database update + + +### 2.6 使用EF Core操作数据库 + +以下是如何使用EF Core来添加、查询、更新和删除数据的示例。 +```cs + class Program + { + static void Main(string[] args) + { + // 创建数据库上下文实例 + using (var context = new MyDbContext()) + { + // 添加数据 + var student = new Student { Name = "张三" }; + context.Students.Add(student); + context.SaveChanges(); + + // 查询数据 + var students = context.Students.ToList(); + foreach (var s in students) + { + Console.WriteLine($"学生ID: {s.Id}, 姓名: {s.Name}"); + } + + // 更新数据 + student.Name = "李四"; + context.SaveChanges(); + + // 删除数据 + context.Students.Remove(student); + context.SaveChanges(); + } + } + } + ``` + +三、代码案例 +### 3.1 添加数据 +```cs + // 添加数据 + var student = new Student { Name = "张三" }; + context.Students.Add(student); + context.SaveChanges(); +``` + +### 3.2 查询数据 +```cs + // 查询数据 + var students = context.Students.ToList(); + foreach (var s in students) + { + Console.WriteLine($"学生ID: {s.Id}, 姓名: {s.Name}"); + } +``` + +### 3.3 更新数据 +```cs + // 更新数据 + student.Name = "李四"; + context.SaveChanges(); +``` + +### 3.4 删除数据 +```cs + // 删除数据 + context.Students.Remove(student); + context.SaveChanges(); +``` \ No newline at end of file -- Gitee