diff --git "a/\345\224\220\345\244\251\345\250\245/20240603-\351\241\271\347\233\256\350\256\276\350\256\2412.md" "b/\345\224\220\345\244\251\345\250\245/20240603-\351\241\271\347\233\256\350\256\276\350\256\2412.md" new file mode 100644 index 0000000000000000000000000000000000000000..7d704cc94796d8971dc61f2829d0adc444993ec0 --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240603-\351\241\271\347\233\256\350\256\276\350\256\2412.md" @@ -0,0 +1,27 @@ +```js +public ICllection GetAllAuthors +{ +//这个实现应该从持久化的数据中心获得:数据库,文件,各种数据异构数据,从各种api获取的数据 + +var list = BookStoreDb.InStance.Authors.ToList(); +var resultList=new List(); +list.ForEach(item=>{ + //直接调用构造函数 + vsar tmp=new AuthorDto{Id,AuthorName=item.AuthorName,Gender=item.Gender}; + }); + resultList.Add(tmp); +} + +public AuthorDto? GetAuthorById(Guid id) +{ + var tmp=BookStoreDb.Instance.Authors.SingleOrDetfault(item=>{ + var tmpResult=tmp!=null?new AuthorDto + {Id,AuthorName=item.AuthorName,Gender=item.Gender}:null; + dynamic xResule=null; + return xRsule;00 + }) + if(tmp!=null){ + xResule=new AuthorDto {Id,AuthorName=item.AuthorName,Gender=item.Gender}; + } +} +``` \ No newline at end of file diff --git "a/\345\224\220\345\244\251\345\250\245/20240604-\351\241\271\347\233\256\350\256\276\350\256\2413.md" "b/\345\224\220\345\244\251\345\250\245/20240604-\351\241\271\347\233\256\350\256\276\350\256\2413.md" new file mode 100644 index 0000000000000000000000000000000000000000..d49ed96b68d251c4c4bbbbc8636b0b4da2082f75 --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240604-\351\241\271\347\233\256\350\256\276\350\256\2413.md" @@ -0,0 +1,135 @@ +### BookRepository.cs +```js +public class BookRepository : IBookRepository +{ + public BookDto? Delete(Guid authorId, Guid BookId) + { + return new BookDto(); + } + + 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) + { + return new BookDto(); + } + + public BookDto? Update(Guid authorId, Guid BookId, BookUpdateDto bookUpdateDto) + { + return new BookDto(); + } +} +``` + +### AuthorRepository.cs +```js +public class AuthorRepository : IAuthorRepository +{ + 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; + } + + 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; + } + } +} + +``` \ No newline at end of file diff --git "a/\345\224\220\345\244\251\345\250\245/20240606-\351\241\271\347\233\256\350\256\276\350\256\2414.md" "b/\345\224\220\345\244\251\345\250\245/20240606-\351\241\271\347\233\256\350\256\276\350\256\2414.md" new file mode 100644 index 0000000000000000000000000000000000000000..517071ccd9bc4c8e822800bfd700182666d97d14 --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240606-\351\241\271\347\233\256\350\256\276\350\256\2414.md" @@ -0,0 +1,17 @@ + +```js +public BookDto? Insert(Guid authorId,BookCreateDto bookCreateDto) +{ + /* + 1.确认传入authorId存在对应的记录 + 如果存在,继续后面的流程,否则返回null + 2.将输入的数据进行整理转换,插入到数据中 + 1.将BookCreateDto类型转换成Books类型数据 + 2.将转换到Books数据插入数据库表 + (为了简单。目前从未对传入数据进行任何数据验证,原因就是为了让整个应用尽量简单,后续一定要进行数据验证) + 1.简单验证 数据类型,是否为空,自符长度,是否一定格式等 + 2.复杂验证 或者叫业务验证 + + */ +} +``` \ No newline at end of file diff --git "a/\345\224\220\345\244\251\345\250\245/20240607-\351\241\271\347\233\256\350\256\276\350\256\241\345\210\240\351\231\244.md" "b/\345\224\220\345\244\251\345\250\245/20240607-\351\241\271\347\233\256\350\256\276\350\256\241\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..a9e999698e7926a19e22e40b67d5ed906e23e9d6 --- /dev/null +++ "b/\345\224\220\345\244\251\345\250\245/20240607-\351\241\271\347\233\256\350\256\276\350\256\241\345\210\240\351\231\244.md" @@ -0,0 +1,118 @@ +### efcore 连接sql server数据库 +在.NET Core中使用Entity Framework Core (EF Core) 连接SQL Server数据库,你需要完成以下几个步骤: + +- 安装必要的NuGet包。 +- 创建数据库上下文类。 +- 配置数据库连接字符串。 +- 使用数据库上下文进行数据库操作。 +- 以下是具体步骤和示例代码: +### 图书删除 +```js +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; + } + +``` +```js +public interface IRepositoryBase +{ + /* + 通过id获取指定模型(对应数据库中的表,模型中的公共属性对应数据表的字段或者叫列) + 获取所有的模型 + + 获取所有实体 + + 新增插入 + + 删除 + + 更新 + + */ + + T GetById(Guid id); + + List GetAll(); + + T Insert(T entity); + T Update(T entity); + T Delete(T entity); + T Delete(Guid id); +} +``` +#### 1. 安装NuGet包 +- 首先,你需要在你的.NET Core项目中安装以下NuGet包: +```js +Microsoft.EntityFrameworkCore +Microsoft.EntityFrameworkCore.SqlServer +你可以在项目目录下打开命令行窗口,使用以下命令安装: + +dotnet add package Microsoft.EntityFrameworkCore +dotnet add package Microsoft.EntityFrameworkCore.SqlServer +``` + +#### 2. 创建数据库上下文类 +接下来,你需要创建一个继承自DbContext的类,这个类将作为你的数据库上下文。 +```js +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来添加一个实体到数据库: +```js +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