{
  "id": "dotnet/ef-core-migration-pending",
  "signature": "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.",
  "signature_zh": "System.InvalidOperationException: 访问数据库时出错。继续运行但没有应用程序的数据库上下文。数据库可能有待处理的迁移。请应用迁移后重试。",
  "regex": "database may have pending migrations\\. Apply migrations and try again",
  "domain": "dotnet",
  "category": "data_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "ASP.NET Core 应用程序因数据库架构与 EF Core 模型不匹配（存在待处理的迁移或数据库缺失）而无法启动。",
  "versions": [
    {
      "version": ".NET 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "EF Core 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "EF Core 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "EF Core 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Delete the database and let EF Core recreate it automatically",
      "why_fails": "Deleting the database loses all data; in production this causes data loss and downtime. The error is about pending migrations, not missing database.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Set EnsureCreated() instead of Migrate() in Program.cs",
      "why_fails": "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.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Remove all migration files and add a new initial migration",
      "why_fails": "Removing migrations loses the migration history; the new initial migration may conflict with existing database state.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Run 'dotnet ef database update' from the project directory to apply all pending migrations.",
      "success_rate": 0.9,
      "how": "Run 'dotnet ef database update' from the project directory to apply all pending migrations.",
      "condition": "",
      "sources": []
    },
    {
      "action": "In Program.cs, use context.Database.Migrate() after building the service provider, wrapped in try-catch for development environments.",
      "success_rate": 0.85,
      "how": "In Program.cs, use context.Database.Migrate() after building the service provider, wrapped in try-catch for development environments.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If using Docker, add a startup script that runs 'dotnet ef database update --connection <connection_string>' before the application starts.",
      "success_rate": 0.88,
      "how": "If using Docker, add a startup script that runs 'dotnet ef database update --connection <connection_string>' before the application starts.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "从项目目录运行 'dotnet ef database update' 以应用所有待处理的迁移。",
    "在 Program.cs 中，在构建服务提供程序后使用 context.Database.Migrate()，并在开发环境中用 try-catch 包裹。",
    "如果使用 Docker，添加启动脚本，在应用程序启动前运行 'dotnet ef database update --connection <连接字符串>'。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/applying",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2023-05-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}