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 0000000000000000000000000000000000000000..26276cf15d55378f6507fdd02ad9da6ba20e3270 --- /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 0000000000000000000000000000000000000000..673b4dd61f7eabe2ef7967fdd545745969b390dc --- /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 0000000000000000000000000000000000000000..2bf45a860a22f9d855a109936cdec6c7485aed22 --- /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 0000000000000000000000000000000000000000..b8383fa27445f174d3c504615e772b3b5b70a0ff --- /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(); +} +```