Overview
RealtimeChat uses PostgreSQL with Entity Framework Core for data persistence. The database includes support for ASP.NET Core Identity, chat rooms, messages with JSONB storage, and full-text search using trigram indexes.PostgreSQL Setup
Create the Database
Connect to PostgreSQL and create the database:Or let Entity Framework create it automatically when running migrations.
Connection Strings
The application uses different connection strings for different environments:- Development
- Docker
- Production
From
appsettings.Development.json:Database Context
TheRealtimeChatDbContext extends ASP.NET Core Identity and includes custom entities:
The context uses snake_case naming convention for database tables and columns, and NoTracking query behavior by default for better performance.
Database Schema
Core Tables
AspNetUsers - User Authentication
AspNetUsers - User Authentication
Stores user accounts with ASP.NET Core Identity:
id(text, PK) - User identifieruser_name(varchar 256) - Usernamenormalized_user_name(varchar 256, indexed) - Normalized usernameemail(varchar 256) - Email addressnormalized_email(varchar 256, indexed) - Normalized emailemail_confirmed(boolean) - Email verification statuspassword_hash(text) - Hashed passwordfirst_name(text) - User’s first namelast_name(text) - User’s last namesecurity_stamp(text) - Security tokenconcurrency_stamp(text) - Concurrency tokenphone_number(text) - Phone numbertwo_factor_enabled(boolean) - 2FA statuslockout_end(timestamptz) - Lockout expirationaccess_failed_count(integer) - Failed login attempts
ChatRooms - Chat Rooms
ChatRooms - Chat Rooms
Stores chat room information:
id(uuid, PK) - Chat room identifiername(text) - Room name- Relationships: One-to-many with Messages and ChatRoomParticipants
Messages - Chat Messages
Messages - Chat Messages
Stores chat messages with JSONB content:
id(uuid, PK) - Message identifieruser_id(text, FK) - Message authorchat_room_id(uuid, FK) - Associated chat roomcontent(jsonb) - Message content (text, images, etc.)sent_at(timestamptz) - Timestamp (default: CURRENT_TIMESTAMP)
Messages use JSONB storage for flexible content types, allowing text messages, images, and other media.
ChatRoomParticipants - Room Membership
ChatRoomParticipants - Room Membership
Maps users to chat rooms:
id(uuid, PK) - Participant record identifierchat_room_id(uuid, FK) - Chat room referenceuser_id(text, FK) - User reference
Identity Tables
ASP.NET Core Identity creates additional tables:AspNetRoles- User rolesAspNetUserRoles- User-role assignmentsAspNetUserClaims- User claimsAspNetUserLogins- External login providersAspNetUserTokens- Authentication tokensAspNetRoleClaims- Role claims
Entity Framework Migrations
Creating Migrations
When you modify entity models, create a new migration:Running Migrations
Key Migrations
InitialCreate - Database Foundation
InitialCreate - Database Foundation
Creates the initial database schema with ASP.NET Identity tables:
AddTrgmIndexToTextMessageContent - Search Optimization
AddTrgmIndexToTextMessageContent - Search Optimization
Adds PostgreSQL trigram index for fast full-text search:
This index enables fast fuzzy text search across message content using PostgreSQL’s trigram similarity.
ChangeMessageContentToJson - JSONB Storage
ChangeMessageContentToJson - JSONB Storage
Converts message content to JSONB for flexible content types:Allows storing various message types (text, images, files) in a single column.
Database Indexes
Performance Indexes
The database includes several indexes for optimal query performance:Seeding Data
Create a data seeding class for development:Backup and Restore
Backup Database
- pg_dump
- Docker
Restore Database
- pg_restore
- Docker
Troubleshooting
Migration fails with permission error
Migration fails with permission error
The
pg_trgm extension requires superuser privileges:Connection refused errors
Connection refused errors
Verify PostgreSQL is running and accepting connections:Check
pg_hba.conf for connection authentication rules.EF Core migrations out of sync
EF Core migrations out of sync
Reset migrations (⚠️ destroys data):Or manually sync:
Best Practices
Use Migrations for Schema Changes
Always create migrations for schema changes instead of modifying the database directly.
Next Steps
- Configuration - Configure application settings
- Docker Deployment - Deploy with Docker
- API Reference - Explore GraphQL API