ASP.NET Core 使用 Entity Framework Core 存取 SQL Server資料庫步驟(資料庫優先)

本篇使用 SQL Server 做示範

範例資料表

在資料庫中建立了用來當作範例的員工資料表:
    
CREATE TABLE Employees
(
    Id   uniqueidentifier PRIMARY KEY DEFAULT newid(),
    Code nvarchar(10)     not null,
    Name nvarchar(100)    not null,
)
    

範例資料:
    
INSERT INTO Employees (Code, Name) VALUES (N'A01', N'小明');
INSERT INTO Employees (Code, Name) VALUES (N'A02', N'大頭');
    

安裝套件

依照資料庫種類安裝對應的 EntityFrameworkCore 套件,這裡示範的是 SQL Server 的套件:
	
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
    

我們使用的是資料庫優先,就是已經建立好資料庫了,依照資料庫的資料表、欄位在 C# 中建立對應的 Entity ,需要再額外安裝 Design 的套件:
	
dotnet add package Microsoft.EntityFrameworkCore.Design
    

建立連接字串

需要連接到資料庫,程式會需要知道連接的資訊,為了避免安全問題和不容易修改,我們將連接資訊記錄在設定檔 appsettings.json 中
    
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost,1433;Database=my_database;User=sa;Password=my_password;Trusted_Connection=False;TrustServerCertificate=true;"
  },
  "AllowedHosts": "*"
}
    


註: 記得替換 ip, port, 資料庫名稱、登入使用者、密碼

同步實體

使用指令使用設定檔中的連接字串依照資料庫建立對應實體,詳細指令使用方式可以查看延伸閱讀:
    
dotnet ef dbcontext scaffold Name=ConnectionStrings:DefaultConnection Microsoft.EntityFrameworkCore.SqlServer -d --context-dir Data/Contexts --context ApplicationDbContext -o Data/Entities -f
    

完成後會在 Entity 資料夾中產生 Table 和 View 對應的實體,並且在 Contexts 資料夾中會產生 ApplicationDbContext.cs ,之後我們都會使用 ApplicationDbContext 來操作資料庫。 延伸閱讀: 利用 dotnet ef 指令依據資料庫定義產生 Entity Framework Core 資料庫實體定義程式碼

依賴注入容器註冊

在 Program.cs 中加入以下程式碼,註冊 ApplicationDbContext:
    
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
);

var app = builder.Build();

    

使用範例

我們建立一個 EmployeeController ,用來示範 新增、修改、刪除、查詢 功能:
    
using Microsoft.AspNetCore.Mvc;
using WebApplication20231104.Data.Contexts;
using WebApplication20231104.Data.Entities;

[ApiController]
[Route("[controller]")]
public class EmployeeController : ControllerBase
{
    private readonly ApplicationDbContext _context;

    public EmployeeController(ApplicationDbContext context)
    {
        _context = context;
    }

    [HttpGet]
    public ActionResult Get()
    {
        var employees = _context.Employees.ToList();
        return Ok(employees);
    }

    public record EmployeeDto(string Code, string Name);

    [HttpPost]
    public ActionResult Post(EmployeeDto dto)
    {
        var employee = new Employee
        {
            Code = dto.Code,
            Name = dto.Name
        };
        _context.Employees.Add(employee);
        _context.SaveChanges();
        return StatusCode(201);
    }

    [HttpPut("{id}")]
    public ActionResult Put(string id, EmployeeDto dto)
    {
        var employee = _context.Employees.FirstOrDefault(x => x.Id.ToString() == id);
        if (employee == null) return NotFound();

        employee.Code = dto.Code;
        employee.Name = dto.Name;
        _context.SaveChanges();
        return Ok();
    }

    [HttpDelete("{id}")]
    public ActionResult Delete(string id)
    {
        var employee = _context.Employees.FirstOrDefault(x => x.Id.ToString() == id);
        if (employee == null) return NotFound();

        _context.Employees.Remove(employee);
        _context.SaveChanges();
        return Ok();
    }
}
    

留言