Bỏ qua, tới nội dung chính

EF Core 10 và PostgreSQL: mapping, migration và truy vấn của Tới Nơi

Bài 21: dùng Npgsql/EF Core 10 với PostgreSQL thật, mapping tiền và UTC, tạo migration có snapshot và kiểm chứng round-trip trong schema lab riêng.

Quản trị viên7 phút đọc

Đây là bài 21 trong loạt C# và .NET 10 qua Tới Nơi. Các ví dụ nhắm tới net10.0, dùng dữ liệu giả và cùng code đã được chạy kiểm chứng trong repository.

Thử trước khi đọc

Gọi EnsureCreated rồi Migrate trên cùng database có phải một quy trình migration an toàn?

Xem đáp án

Không. EnsureCreated và migration phục vụ các đường tạo schema khác nhau; lab dùng migration nhất quán từ đầu.

Từ HTTP đúng tới dữ liệu lưu đúng

API báo giá từ chặng trước chưa lưu trạng thái. Bây giờ ta bắt đầu persistence: một chuyến có identity, khách, cước, thời điểm tạo và tài xế (có thể chưa có). EF Core ánh xạ object sang mô hình quan hệ; Npgsql là provider nối EF Core với PostgreSQL.

Ta dùng EF Core 10 và provider Npgsql 10, không phải EF6 cũ. Có DbContext không có nghĩa là mọi biểu thức C# tự động thành SQL tốt; mapping, query và transaction vẫn phải được xem xét và kiểm chứng. Bài này dùng PostgreSQL 18 thật cho lab, không dùng InMemory provider để suy ra hành vi cột/constraint.

Project persistence phục vụ bài 21–26 nên chứa cả các bảng sẽ được giải thích ở bài về idempotency/Outbox. TripRow là persistence model để quan sát cơ chế; nó chưa phải Trip aggregate có hành vi ở chặng DDD. Các setter public của model này không thay đổi định hướng encapsulation của domain.

Mapping là hợp đồng giữa hai hệ kiểu

Fare được cấu hình numeric(18,2), CreatedAt ánh xạ thời điểm có offset theo provider; lab ghi UTC. DriverId nullable, Status được giới hạn độ dài, quan hệ Trip–Customer có foreign key và index theo CustomerId/CreatedAt. Cột numeric giữ độ chính xác theo scale, không tự chọn quy tắc làm tròn nghiệp vụ.

Test case round-trip cố ý ghi 14255.50 để kiểm tra khả năng lưu decimal có phần lẻ; test case này không đổi policy cước nguyên VND từ bài 14. Nếu model nhận nhiều chữ số hơn scale, database có thể làm tròn theo kiểu cột, nên phải kiểm soát precision trước khi lưu khi hợp đồng yêu cầu.

UTC cần nhất quán tại ranh giới ghi/đọc. PostgreSQL timestamp with time zone không lưu lại tên múi giờ gốc của khách; nếu nghiệp vụ lịch tương lai cần timezone ID, phải có field riêng. Đừng suy ra rằng kiểu cột thời gian đã giải quyết mọi câu hỏi về DST.

Persistence modelKiểu + mappingMigration SQLSchema + constraintPostgreSQLGiá trị lưu thực tế
Hình 1. Mapping và migration cần đi tới database thật; model C# đúng chưa chứng minh cột và constraint đã đúng.

DbContext là một đơn vị theo dõi ngắn hạn

Context giữ change tracker, cấu hình mapping và đơn vị SaveChanges. Tạo context ngắn cho một use case hoặc phạm vi phù hợp, dispose sau khi dùng. Không dùng cùng một context cho nhiều thao tác song song rồi mong EF tự khoá giúp.

Trong test, instance ghi được dispose trước khi một instance mới đọc lại. Cách này tránh để cache của tracker che mất lỗi mapping và lỗi serialization ở provider. Bài 22 sẽ đi sâu vào tracking, projection và SQL; hiện chỉ dùng AsNoTracking cho query kiểm tra dữ liệu đã lưu.

Context đã có nhiều cơ chế giống Unit of Work, nhưng kiến trúc đích vẫn cần ranh giới application/domain rõ. Không vội bọc mọi method EF thành một generic repository chỉ để có thêm interface ở bài nhập môn dữ liệu.

Migration là lịch sử có thể review

Migrations và ModelSnapshot do dotnet-ef 10.0.10 sinh ra từ model và được giữ trong repository. Đừng dùng EnsureCreated trong test rồi bỏ qua migration: ta muốn kiểm schema đúng theo đường mà ứng dụng sẽ dùng.

Test tạo schema tên ngẫu nhiên trong database lab, đặt SearchPath cho connection và bảng lịch sử migration trong schema đó, rồi MigrateAsync. Model không hard-code tên schema test; mỗi test case có không gian riêng. Khi xong, chỉ dọn schema do fixture tạo, không gọi EnsureDeleted để xoá cả database.

Migration kế tiếp cần so với snapshot để sinh delta. Review SQL, thời gian khoá, dữ liệu có sẵn và kế hoạch rollback trước khi áp dụng production. Lab tự migrate lúc dựng fixture để kiểm chứng, không phải khuyến cáo để mỗi replica production tranh nhau chạy migration lúc startup.

Test riêngSchema lab_GUIDMigrate + SaveCác bước kiểm chứngContext đọc mớiKiểm round-trip
Hình 2. Mỗi test case sở hữu một schema trong database lab, không xoá database hoặc dùng schema dữ liệu thật.

Ba test case có ý nghĩa gì?

Test case round-trip kiểm tra fare, instant UTC, DriverId null và không còn migration pending. Test case chạy migration lần nữa yêu cầu dữ liệu còn nguyên. Test case foreign key ghi một CustomerId không tồn tại phải thất bại; object có Guid hợp lệ về cú pháp không có nghĩa là khách đó tồn tại.

Bài tập: nếu vừa dùng context ghi để đọc lại cùng entity, vì sao có thể bỏ sót lỗi database? Tại sao fixture không xoá cả database khi dọn?

Xem đáp án

Tracker có thể trả về instance đã có sẵn mà không tạo ra một lượt round-trip độc lập đúng như ý định kiểm tra. Fixture chỉ sở hữu schema GUID của mình; xoá cả database vượt phạm vi và có thể phá dữ liệu của test case khác.

Chạy ví dụ và kiểm chứng

Cần PostgreSQL local và database riêng tên toi_noi_dotnet_lab. Thiết lập biến môi trường TOI_NOI_PG bằng connection string của lab trước khi chạy; không đưa mật khẩu vào source. README của persistence lab có hướng dẫn. Fixture từ chối database khác tên hoặc host ngoài localhost, không tự bỏ qua test mà không báo lỗi khi thiếu kết nối; fixture chỉ tạo và dọn schema GUID riêng.

Từ thư mục gốc repository:

dotnet test examples/toi-noi-dotnet/21-efcore-postgres/Persistence.Tests

Migration và snapshot nằm ở examples/toi-noi-dotnet/21-efcore-postgres/Persistence/Migrations. Để dùng CLI EF cục bộ, chuyển vào examples/toi-noi-dotnet, chạy dotnet tool restore rồi dotnet ef migrations list --project 21-efcore-postgres/Persistence. Không trộn EnsureCreated vào đường migration này.

TripDb.cs — 21-efcore-postgres/Persistence/TripDb.cs

using Microsoft.EntityFrameworkCore;
namespace ToiNoi.Persistence;
public sealed class CustomerRow
{
    public Guid Id { get; set; }
    public string Name { get; set; } = "";
    public List<TripRow> Trips { get; set; } = [];
}
public sealed class TripRow
{
    public Guid Id { get; set; }
    public Guid CustomerId { get; set; }
    public CustomerRow Customer { get; set; } = null!;
    public decimal Fare { get; set; }
    public string Status { get; set; } = "requested";
    public string? DriverId { get; set; }
    public DateTimeOffset CreatedAt { get; set; }
    public Guid Version { get; set; } = Guid.NewGuid();
}
public sealed class BookingRequestRow
{
    public Guid CustomerId { get; set; }
    public string Key { get; set; } = "";
    public decimal DistanceKm { get; set; }
    public Guid TripId { get; set; }
}
public sealed class OutboxRow
{
    public Guid Id { get; set; }
    public Guid TripId { get; set; }
    public string Payload { get; set; } = "";
    public bool Published { get; set; }
}
public sealed class InboxRow
{
    public string Consumer { get; set; } = "";
    public Guid EventId { get; set; }
}
public sealed class ReceiptRow
{
    public Guid EventId { get; set; }
    public Guid TripId { get; set; }
}
public sealed class TripDb(DbContextOptions<TripDb> options) : DbContext(options)
{
    public DbSet<CustomerRow> Customers => Set<CustomerRow>();
    public DbSet<TripRow> Trips => Set<TripRow>();
    public DbSet<BookingRequestRow> BookingRequests => Set<BookingRequestRow>();
    public DbSet<OutboxRow> Outbox => Set<OutboxRow>();
    public DbSet<InboxRow> Inbox => Set<InboxRow>();
    public DbSet<ReceiptRow> Receipts => Set<ReceiptRow>();
    protected override void OnModelCreating(ModelBuilder model)
    {
        model.Entity<CustomerRow>(e => { e.ToTable("customers"); e.HasKey(x => x.Id); e.Property(x => x.Name).HasMaxLength(120); });
        model.Entity<TripRow>(e =>
        {
            e.ToTable("trips"); e.HasKey(x => x.Id);
            e.Property(x => x.Fare).HasPrecision(18, 2);
            e.Property(x => x.Status).HasMaxLength(32);
            e.Property(x => x.DriverId).HasMaxLength(64);
            e.Property(x => x.Version).IsConcurrencyToken();
            e.HasIndex(x => new { x.CustomerId, x.CreatedAt });
            e.HasOne(x => x.Customer).WithMany(x => x.Trips).HasForeignKey(x => x.CustomerId);
        });
        model.Entity<BookingRequestRow>(e =>
        {
            e.ToTable("booking_requests"); e.HasKey(x => new { x.CustomerId, x.Key });
            e.Property(x => x.Key).HasMaxLength(128); e.Property(x => x.DistanceKm).HasPrecision(12, 6);
        });
        model.Entity<OutboxRow>(e => { e.ToTable("outbox"); e.HasKey(x => x.Id); e.Property(x => x.Payload).HasColumnType("text"); });
        model.Entity<InboxRow>(e => { e.ToTable("inbox"); e.HasKey(x => new { x.Consumer, x.EventId }); e.Property(x => x.Consumer).HasMaxLength(64); });
        model.Entity<ReceiptRow>(e => { e.ToTable("receipts"); e.HasKey(x => x.EventId); });
    }
}

MappingTests.cs — 21-efcore-postgres/Persistence.Tests/MappingTests.cs

using Microsoft.EntityFrameworkCore;
using ToiNoi.Persistence;
using Xunit;
namespace ToiNoi.LabTests;
public sealed class MappingTests
{
    [Fact]
    public async Task Migration_round_trip_preserves_decimal_utc_and_nullable_driver()
    {
        await using var lab = await PgLab.CreateAsync(); var customer = await lab.SeedAsync();
        Guid id;
        await using (var write = lab.Open())
        {
            var trip = new TripRow { Id = Guid.NewGuid(), CustomerId = customer, Fare = 14255.50m, CreatedAt = DateTimeOffset.UnixEpoch };
            write.Trips.Add(trip); await write.SaveChangesAsync(); id = trip.Id;
        }
        await using var read = lab.Open(); var restored = await read.Trips.AsNoTracking().SingleAsync(x => x.Id == id);
        Assert.Equal(14255.50m, restored.Fare); Assert.Equal(DateTimeOffset.UnixEpoch, restored.CreatedAt); Assert.Null(restored.DriverId);
        Assert.Empty(await read.Database.GetPendingMigrationsAsync());
    }
    [Fact]
    public async Task Migration_can_be_applied_again_without_losing_rows()
    {
        await using var lab = await PgLab.CreateAsync(); await lab.SeedAsync();
        await using var db = lab.Open(); await db.Database.MigrateAsync(); Assert.Equal(3, await db.Trips.CountAsync());
    }
    [Fact]
    public async Task Foreign_key_rejects_unknown_customer()
    {
        await using var lab = await PgLab.CreateAsync(); await using var db = lab.Open();
        db.Trips.Add(new TripRow { Id = Guid.NewGuid(), CustomerId = Guid.NewGuid(), CreatedAt = DateTimeOffset.UnixEpoch });
        await Assert.ThrowsAsync<DbUpdateException>(() => db.SaveChangesAsync());
    }
}

Bài trước: bài 20. Bài tiếp: bài 22.

Tài liệu gốc

Bạn thấy bài viết này thế nào?

Chọn một hoặc nhiều cảm xúc. Bấm lần nữa để bỏ chọn.

Bình luận (…)