Database Setup
This architecture supports multiple database providers through a unified repository pattern. You can use SQL Server, MySQL, MariaDB, or MongoDB.Configuration
Database Provider Selection
Configure your database provider inappsettings.json:
"sqlserver"- Microsoft SQL Server"mysql"- MySQL"mariadb"- MariaDB"mongodb"- MongoDB
Template-API/appsettings.json:9-15
SQL Server Setup
Configure Connection String
Update the SQL connection string in For Azure SQL:For Remote SQL Server:
appsettings.json:Create DbContext
Your DbContext should inherit from Reference:
DbContext and define entity sets:Infrastructure/Repositories/Sql/StoreDbContext.cs:10-31Create Initial Migration
Navigate to the Infrastructure project and create your first migration:This creates a
Migrations folder with migration files:<timestamp>_InitialCreate.cs- Migration operations<timestamp>_InitialCreate.Designer.cs- Migration metadataStoreDbContextModelSnapshot.cs- Current model state
MongoDB Setup
Configure MongoDB Connection
Update the MongoDB connection string in For local MongoDB:Reference:
appsettings.json:Template-API/appsettings.json:13Repository Implementation
SQL Repository
MongoDB Repository
Common Migration Commands
Create Migration
Create Migration
InitialCreate- First migrationAddPropertyToEntity- Adding propertiesCreateTableName- New tablesRemoveColumnName- Removing columns
Apply Migrations
Apply Migrations
View Migrations
View Migrations
Remove Migration
Remove Migration
Database Initialization
Seed Data
Create a seeding service for initial data:Call Seeder in Program.cs
Troubleshooting
Migration Build Failed
Migration Build Failed
Error: Build failed before running migrationsSolution:
- Ensure your project compiles:
dotnet build - Check that startup project is specified
- Verify all NuGet packages are restored
Connection String Issues
Connection String Issues
Error: Cannot connect to databaseSolution:
- Verify connection string format
- Check database server is running
- Test connection with SQL Management Studio or similar tool
- Ensure firewall allows connection
- For LocalDB, verify it’s installed:
sqllocaldb info
Entity Not Included in Model
Entity Not Included in Model
Error: Entity type not included in the modelSolution:
- Add DbSet to DbContext:
- Configure in OnModelCreating:
- Create new migration
Next Steps
Creating Entities
Learn how to create domain entities
Event Handling
Implement domain and integration events