# System.InvalidOperationException: An error occurred while accessing the database. Continuing without the application's database context. The database may have pending migrations. Apply migrations and try again.

- **ID:** `dotnet/ef-core-migration-pending`
- **Domain:** dotnet
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 92%

## Root Cause

ASP.NET Core application fails to start because the database schema does not match the EF Core model due to pending migrations or missing database.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| .NET 6.0 | active | — | — |
| .NET 7.0 | active | — | — |
| .NET 8.0 | active | — | — |
| EF Core 6.0 | active | — | — |
| EF Core 7.0 | active | — | — |
| EF Core 8.0 | active | — | — |

## Workarounds

1. **Run 'dotnet ef database update' from the project directory to apply all pending migrations.** (90% success)
   ```
   Run 'dotnet ef database update' from the project directory to apply all pending migrations.
   ```
2. **In Program.cs, use context.Database.Migrate() after building the service provider, wrapped in try-catch for development environments.** (85% success)
   ```
   In Program.cs, use context.Database.Migrate() after building the service provider, wrapped in try-catch for development environments.
   ```
3. **If using Docker, add a startup script that runs 'dotnet ef database update --connection <connection_string>' before the application starts.** (88% success)
   ```
   If using Docker, add a startup script that runs 'dotnet ef database update --connection <connection_string>' before the application starts.
   ```

## Dead Ends

- **Delete the database and let EF Core recreate it automatically** — Deleting the database loses all data; in production this causes data loss and downtime. The error is about pending migrations, not missing database. (95% fail)
- **Set EnsureCreated() instead of Migrate() in Program.cs** — EnsureCreated() skips migrations and creates a schema based on the current model, but it will fail if the database already exists with a different schema. (80% fail)
- **Remove all migration files and add a new initial migration** — Removing migrations loses the migration history; the new initial migration may conflict with existing database state. (75% fail)
