Overview
ESP Santa Fe de Antioquia uses Entity Framework Core 3.1 with SQL Server for data persistence. This guide covers database setup, running migrations, and production database management.Database Requirements
- SQL Server: Version 2012 or later (SQL Server 2016+ recommended)
- Database Name:
BDESPSantaFeAnt - Collation: SQL_Latin1_General_CP1_CI_AS (default)
- Recovery Model: Full (recommended for production)
- Compatibility Level: 130 (SQL Server 2016) or higher
Database Schema Overview
The application database includes these main tables:- AspNetUsers/Roles/Claims - ASP.NET Core Identity for authentication
- Categories - Service categories
- Masters - Master data for brigades, blogs, and bidding processes
- BiddingParticipants - Participants in bidding processes
- Documents - Legal documents and files
- FileDocuments - Document file attachments
- PQRSDs - Citizen requests and inquiries (Peticiones, Quejas, Reclamos, Sugerencias, Denuncias)
- Products - Services offered by the organization
- Employees - Employee information
- Brigades - Brigade management (part of Masters)
The database schema is defined in the migration file at
src/persistenDatabase/Migrations/20210927150752_CreandoBD.cs.Creating the Production Database
Connect to SQL Server
Connect to your SQL Server instance using SQL Server Management Studio (SSMS) or Azure Data Studio.
Understanding Entity Framework Migrations
Entity Framework Core uses migrations to manage database schema changes. The application has one base migration:- 20210927150752_CreandoBD - Initial database creation
src/persistenDatabase/Migrations/.
How Migrations Work
- Migration File (
20210927150752_CreandoBD.cs) - ContainsUp()andDown()methods - Designer File (
20210927150752_CreandoBD.Designer.cs) - Metadata about the migration - Model Snapshot (
ApplicationDbContextModelSnapshot.cs) - Current state of the model
__EFMigrationsHistory table.
Running Database Migrations
Prerequisites
Ensure you have the Entity Framework Core tools installed:Apply Migrations to Production
Apply Migrations
Run the update command:Or using the short form:This will:
- Connect to the database
- Check
__EFMigrationsHistoryfor applied migrations - Apply any pending migrations in order
Migration with Specific Environment
appsettings.Production.json configuration file.
If the migration fails, check the error message carefully. Common issues include incorrect connection strings, insufficient permissions, or network connectivity problems.
Alternative: SQL Script Generation
For environments where the application account doesn’t have DDL permissions, generate SQL scripts:Review the Script
Open
migration.sql and review the SQL commands. The script will:- Check for existing migrations
- Only apply changes that haven’t been applied
- Update the
__EFMigrationsHistorytable
Script-Specific Migration Range
To generate a script for specific migrations:Verifying Database Setup
After applying migrations, verify the database:Check Tables
- AspNetRoles
- AspNetRoleClaims
- AspNetUsers
- AspNetUserClaims
- AspNetUserLogins
- AspNetUserRoles
- AspNetUserTokens
- BiddingParticipants
- Categories
- Documents
- Employees
- FileDocuments
- Masters
- PQRSDs
- Products
- __EFMigrationsHistory
Verify Indexes
Test Application Connection
Update yourappsettings.Production.json and start the application:
Database Maintenance
Backup Strategy
Implement a backup strategy for production:- Full backup: Daily
- Transaction log backup: Every 15-30 minutes
- Keep backups for at least 30 days
Database Monitoring
Monitor database performance:Index Maintenance
Rebuild indexes periodically:Troubleshooting
Migration Fails with “Login failed”
Problem: Connection string is incorrect or user lacks permissions. Solution:- Verify connection string in
appsettings.Production.json - Test connection using SQL Server Management Studio
- Ensure user has
db_ddladminrole
”Cannot open database” Error
Problem: Database doesn’t exist or service can’t access it. Solution:“Network-related or instance-specific error”
Problem: SQL Server is not accessible from the application server. Solution:- Check firewall rules (SQL Server uses port 1433 by default)
- Verify SQL Server allows remote connections
- Ensure SQL Server Browser service is running
- Test with
telnet YOUR_SERVER 1433
”MultipleActiveResultSets not supported”
Problem: Connection string is missing required parameter. Solution: AddMultipleActiveResultSets=true to connection string.
Creating Future Migrations
When you need to modify the database schema:Security Best Practices
- Use strong passwords - Minimum 12 characters with mixed case, numbers, and symbols
- Limit permissions - Application account should have minimal required permissions
- Enable encryption - Add
Encrypt=Trueto connection strings for Azure SQL - Firewall rules - Only allow connections from application server IPs
- Audit access - Enable SQL Server auditing for production databases
- Regular backups - Test backup restoration regularly
- Connection pooling - Keep connection strings consistent to enable pooling
- Secrets management - Never store connection strings in source control
Database Connection Pooling
Entity Framework Core uses connection pooling by default. Configure in connection string:- Min Pool Size: 5-10
- Max Pool Size: 50-100 (adjust based on load)
Next Steps
After setting up your database:- Create an admin user account
- Seed initial data (categories, products, etc.)
- Set up database monitoring
- Configure automated backups
- Test the application end-to-end
Database Setup Checklist
- Created
BDESPSantaFeAntdatabase - Created application SQL login with appropriate permissions
- Tested database connectivity
- Applied Entity Framework migrations
- Verified all tables were created
- Checked migration history table
- Set up automated backup schedule
- Configured transaction log backups
- Set database recovery model to Full
- Documented connection strings securely
- Configured firewall rules
- Tested application database connection
- Set up database monitoring
- Created initial admin user account