diff --git "a/\346\235\216\345\200\241\347\246\217/20240531-\346\216\245\345\217\243.md" "b/\346\235\216\345\200\241\347\246\217/20240531-\346\216\245\345\217\243.md" new file mode 100644 index 0000000000000000000000000000000000000000..eaa99df878ad8616231ced04a05d41c67eb99682 --- /dev/null +++ "b/\346\235\216\345\200\241\347\246\217/20240531-\346\216\245\345\217\243.md" @@ -0,0 +1,54 @@ +## 接口 +- I实例名Repository + ```csharp + //通过Id获取所有作者的实现 + 实例对象? GetAuthorById(Guid guid); + + //获取所有作者的方法 + //函数三要素(函数名,形参,返回值) + ICollection<实例对象> GetAllAuthors(); + + ``` +- 实例名Repository + ```csharp + public class 实例名Repository:I实例名Repository + { + public ICollection<实例名> GetAllAuthors() + { + //从持久化数据库中获取 + return 名Db.Instance.实例名.ToList(); + //return [.. 名Db.Instance.实例名]; + } + public 实例名? GetAuthorById(Guid guid) + { + return 名Db.Instance.实例名.SingleOrDefault(item=>item.Id==guid); + } + } + ``` +- 控制器中 + ```csharp + private readonly I实例名Repository _实例名Repository; + public 实例名Controller(I实例名Repository 实例名Repository) + { + _实例名Repository=实例名Repository; + } + //获取get的函数中 + [HttpGet("{id?}")] + public IActionResult Get(int id) + { + if(id.ToString==""){ + return Ok(_实例名Repository.GetAllAuthors()); + }else{ + return Ok(_实例名Repository.GetAuthorById(id)); + } + } + ``` +- Startup + ```csharp + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + //新加 + services.AddScoped(); + } + ``` \ No newline at end of file diff --git "a/\346\235\216\345\200\241\347\246\217/20240603--\351\241\271\347\233\256.md" "b/\346\235\216\345\200\241\347\246\217/20240603--\351\241\271\347\233\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..61c62bb5831fa7d792593e93519050dc544fa4a8 --- /dev/null +++ "b/\346\235\216\345\200\241\347\246\217/20240603--\351\241\271\347\233\256.md" @@ -0,0 +1,30 @@ +```csharp +public class ProductEditDto +{ + public string Name { get; set; } + public decimal Price { get; set; } +} +``` + +然后,在控制器中使用DTO来接收和验证编辑请求: + +```csharp +[HttpPut("{id}")] +public async Task UpdateProduct(int id, ProductEditDto productDto) +{ + var existingProduct = await _context.Products.FindAsync(id); + + if (existingProduct == null) + { + return NotFound(); + } + + existingProduct.Name = productDto.Name; + existingProduct.Price = productDto.Price; + + _context.Products.Update(existingProduct); + await _context.SaveChangesAsync(); + + return NoContent(); +} +``` \ No newline at end of file diff --git "a/\346\235\216\345\200\241\347\246\217/20240604--\351\241\271\347\233\2562.md" "b/\346\235\216\345\200\241\347\246\217/20240604--\351\241\271\347\233\2562.md" new file mode 100644 index 0000000000000000000000000000000000000000..118dd2d0a0feaadb4d586f6836fadcb8c60685d7 --- /dev/null +++ "b/\346\235\216\345\200\241\347\246\217/20240604--\351\241\271\347\233\2562.md" @@ -0,0 +1,17 @@ +```csharp +[HttpDelete("{id}")] +public async Task DeleteProduct(int id) +{ + var existingProduct = await _context.Products.FindAsync(id); + + if (existingProduct == null) + { + return NotFound(); + } + + _context.Products.Remove(existingProduct); + await _context.SaveChangesAsync(); + + return NoContent(); +} +``` \ No newline at end of file diff --git "a/\346\235\216\345\200\241\347\246\217/20240606--\351\241\271\347\233\2563.md" "b/\346\235\216\345\200\241\347\246\217/20240606--\351\241\271\347\233\2563.md" new file mode 100644 index 0000000000000000000000000000000000000000..e0b0959f916cc7108d3157ab79a41ec343620d13 --- /dev/null +++ "b/\346\235\216\345\200\241\347\246\217/20240606--\351\241\271\347\233\2563.md" @@ -0,0 +1,38 @@ +```bash +Install-Package Microsoft.EntityFrameworkCore +Install-Package Microsoft.EntityFrameworkCore.Design +``` + + +```csharp +public class Product +{ + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } +} +``` + + +```csharp +public class AppDbContext : DbContext +{ + public DbSet Products { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer("YourConnectionString"); + } +} +``` + + + +```csharp +using (var context = new AppDbContext()) +{ + var product = new Product { Name = "Sample Product", Price = 10.99 }; + context.Products.Add(product); + context.SaveChanges(); +} +``` diff --git "a/\346\235\216\345\200\241\347\246\217/20240607--\351\241\271\347\233\2564.md" "b/\346\235\216\345\200\241\347\246\217/20240607--\351\241\271\347\233\2564.md" new file mode 100644 index 0000000000000000000000000000000000000000..9b0f63156db5b2abef75b2e9fe2c8328c4726aef --- /dev/null +++ "b/\346\235\216\345\200\241\347\246\217/20240607--\351\241\271\347\233\2564.md" @@ -0,0 +1,28 @@ +```csharp +// 定义数据模型类 +public class Product +{ + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } +} + +// 创建DbContext +public class AppDbContext : DbContext +{ + public DbSet Products { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer("YourConnectionString"); + } +} + +// 使用DbContext进行数据库操作 +using (var context = new AppDbContext()) +{ + var product = new Product { Name = "Sample Product", Price = 10.99 }; + context.Products.Add(product); + context.SaveChanges(); +} +``` \ No newline at end of file