From fd9ab9a339dcc2f452776da6c4725aff7d99c64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E7=8E=89=E6=9D=AD?= <2150097635@example.com> Date: Sun, 9 Jun 2024 22:33:55 +0800 Subject: [PATCH] , --- .../20240603_\351\241\271\347\233\2561.md" | 33 +++++++++++++++ .../20240604_\351\241\271\347\233\2562.md" | 18 +++++++++ .../20240606_\351\241\271\347\233\2563 .md" | 40 +++++++++++++++++++ .../20240607_\351\241\271\347\233\2564.md" | 28 +++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 "\351\273\204\347\216\211\346\235\255/20240603_\351\241\271\347\233\2561.md" create mode 100644 "\351\273\204\347\216\211\346\235\255/20240604_\351\241\271\347\233\2562.md" create mode 100644 "\351\273\204\347\216\211\346\235\255/20240606_\351\241\271\347\233\2563 .md" create mode 100644 "\351\273\204\347\216\211\346\235\255/20240607_\351\241\271\347\233\2564.md" diff --git "a/\351\273\204\347\216\211\346\235\255/20240603_\351\241\271\347\233\2561.md" "b/\351\273\204\347\216\211\346\235\255/20240603_\351\241\271\347\233\2561.md" new file mode 100644 index 0000000..26276cf --- /dev/null +++ "b/\351\273\204\347\216\211\346\235\255/20240603_\351\241\271\347\233\2561.md" @@ -0,0 +1,33 @@ + + +```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(); +} +``` + diff --git "a/\351\273\204\347\216\211\346\235\255/20240604_\351\241\271\347\233\2562.md" "b/\351\273\204\347\216\211\346\235\255/20240604_\351\241\271\347\233\2562.md" new file mode 100644 index 0000000..673b4dd --- /dev/null +++ "b/\351\273\204\347\216\211\346\235\255/20240604_\351\241\271\347\233\2562.md" @@ -0,0 +1,18 @@ + +```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(); +} +``` diff --git "a/\351\273\204\347\216\211\346\235\255/20240606_\351\241\271\347\233\2563 .md" "b/\351\273\204\347\216\211\346\235\255/20240606_\351\241\271\347\233\2563 .md" new file mode 100644 index 0000000..2bf45a8 --- /dev/null +++ "b/\351\273\204\347\216\211\346\235\255/20240606_\351\241\271\347\233\2563 .md" @@ -0,0 +1,40 @@ + +```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/\351\273\204\347\216\211\346\235\255/20240607_\351\241\271\347\233\2564.md" "b/\351\273\204\347\216\211\346\235\255/20240607_\351\241\271\347\233\2564.md" new file mode 100644 index 0000000..b8383fa --- /dev/null +++ "b/\351\273\204\347\216\211\346\235\255/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(); +} +``` -- Gitee