DbContext đại diện cho một session với database, quản lý entity instances, change tracking và SaveChanges().
- Nó theo dõi tất cả entities đã tải (added, modified, deleted).
- Implement
IDisposable— luôn dùngusingstatement. - Trong ASP.NET Core được scope theo mỗi request.
- Không bao giờ dùng static hay singleton
DbContext— luôn dùng scoped lifetime.
DbContext represents a session with the database, managing entity instances, change tracking, and SaveChanges() operations.
- It tracks all loaded entities (added, modified, deleted) and implements
IDisposable— always use ausingstatement. - In ASP.NET Core it is scoped per request.
- Never use a static or singleton
DbContext.