asp.net core 6.0 efcore +sqlserver增刪改查的demo
下面是一個使用ASP.NET Core 5.0和Entity Framework Core進(jìn)行增刪改查操作的示例。
首先,創(chuàng)建一個空的ASP.NET Core 6.0 Web應(yīng)用程序項目。
然后,安裝以下NuGet包:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
接下來,創(chuàng)建一個數(shù)據(jù)庫上下文類,用于定義實(shí)體類和數(shù)據(jù)庫連接配置。在項目中創(chuàng)建一個名為AppDbContext.cs的文件,并添加以下代碼:
using Microsoft.EntityFrameworkCore;
namespace EFCoreDemo.Models
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
}
}
然后,創(chuàng)建一個實(shí)體類來表示數(shù)據(jù)庫表。在項目中創(chuàng)建一個名為Customer.cs的文件,并添加以下代碼:
namespace EFCoreDemo.Models
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
}
接下來,配置數(shù)據(jù)庫連接。打開appsettings.json文件,并添加以下內(nèi)容:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EFCoreDemo;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
然后,在Startup.cs文件的ConfigureServices方法中添加以下代碼,用于配置數(shù)據(jù)庫上下文的依賴注入:
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
接下來,創(chuàng)建一個控制器類來處理增刪改查操作。在項目中創(chuàng)建一個名為CustomersController.cs的文件,并添加以下代碼:
using EFCoreDemo.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace EFCoreDemo.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class CustomersController : ControllerBase
{
private readonly AppDbContext _dbContext;
public CustomersController(AppDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Customer>>> GetCustomers()
{
return await _dbContext.Customers.ToListAsync();
}
[HttpGet("{id}")]
public async Task<ActionResult<Customer>> GetCustomer(int id)
{
var customer = await _dbContext.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
}
return customer;
}
[HttpPost]
public async Task<ActionResult<Customer>> CreateCustomer(Customer customer)
{
_dbContext.Customers.Add(customer);
await _dbContext.SaveChangesAsync();
return CreatedAtAction(nameof(GetCustomer), new { id = customer.Id }, customer);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateCustomer(int id, Customer customer)
{
if (id != customer.Id)
{
return BadRequest();
}
_dbContext.Entry(customer).State = EntityState.Modified;
try
{
await _dbContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!_dbContext.Customers.Any(c => c.Id == id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCustomer(int id)
{
var customer = await _dbContext.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
}
_dbContext.Customers.Remove(customer);
await _dbContext.SaveChangesAsync();
return NoContent();
}
}
}
最后,運(yùn)行應(yīng)用程序,并使用工具(例如Postman)測試增刪改查操作。以下是一些示例請求的URL和請求體:文章來源:http://www.zghlxwxcb.cn/news/detail-642342.html
GET /api/customers:獲取所有客戶
GET /api/customers/{id}:根據(jù)ID獲取客戶
POST /api/customers:創(chuàng)建客戶 請求體:
json
{
“name”: “John Doe”,
“email”: “john@example.com”
}
PUT /api/customers/{id}:更新客戶 請求體:
json
{
“id”: 1,
“name”: “John Doe”,
“email”: “john.doe@example.com”
}
DELETE /api/customers/{id}:刪除客戶
希望這個示例能幫助你開始使用ASP.NET Core 6.0和Entity Framework Core進(jìn)行增刪改查操作。文章來源地址http://www.zghlxwxcb.cn/news/detail-642342.html
到了這里,關(guān)于asp.net core 6.0 efcore +sqlserver增刪改查的demo的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!