{
  "id": "dotnet/ef-core-sync-query-on-async-context",
  "signature": "System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.",
  "signature_zh": "System.InvalidOperationException: 在此上下文实例上完成上一个操作之前，又启动了第二个操作。这通常是由不同线程同时使用同一 DbContext 实例引起的。",
  "regex": "System\\.InvalidOperationException: A second operation was started on this context instance before a previous operation completed\\. This is usually caused by different threads concurrently using the same instance of DbContext\\.",
  "domain": "dotnet",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A DbContext instance is not thread-safe and is being used concurrently from multiple threads, typically due to synchronous calls blocking async operations or improper DI registration (e.g., Singleton instead of Scoped).",
  "root_cause_type": "generic",
  "root_cause_zh": "DbContext 实例不是线程安全的，多个线程同时使用它，通常是由于同步调用阻塞异步操作或 DI 注册不当（例如，使用 Singleton 代替 Scoped）导致的。",
  "versions": [
    {
      "version": "Entity Framework Core 3.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Entity Framework Core 5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Entity Framework Core 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Entity Framework Core 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Entity Framework Core 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Changing DbContext registration to Singleton to reuse the same instance globally.",
      "why_fails": "Singleton DbContext is not thread-safe and will cause the same error under concurrent requests; it also leads to stale data and memory leaks.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Adding locks around all DbContext usage to serialize access.",
      "why_fails": "Locks defeat the purpose of async I/O and can cause deadlocks or severe performance degradation under load.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting DbContext.PoolSize to a large number without configuring AddDbContextPool.",
      "why_fails": "PoolSize only applies when using DbContext pooling; without AddDbContextPool, it has no effect.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure DbContext is registered as Scoped in DI (default). For ASP.NET Core: services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));",
      "success_rate": 0.9,
      "how": "Ensure DbContext is registered as Scoped in DI (default). For ASP.NET Core: services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));",
      "condition": "",
      "sources": []
    },
    {
      "action": "Avoid mixing sync and async calls on the same DbContext. Use async methods (e.g., ToListAsync() instead of ToList()) consistently. If you must use sync, create a separate DbContext instance.",
      "success_rate": 0.85,
      "how": "Avoid mixing sync and async calls on the same DbContext. Use async methods (e.g., ToListAsync() instead of ToList()) consistently. If you must use sync, create a separate DbContext instance.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use DbContext pooling for high-throughput scenarios: services.AddDbContextPool<MyDbContext>(options => options.UseSqlServer(connectionString), poolSize: 128);",
      "success_rate": 0.8,
      "how": "Use DbContext pooling for high-throughput scenarios: services.AddDbContextPool<MyDbContext>(options => options.UseSqlServer(connectionString), poolSize: 128);",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Ensure DbContext is registered as Scoped in DI (default). For ASP.NET Core: services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));",
    "Avoid mixing sync and async calls on the same DbContext. Use async methods (e.g., ToListAsync() instead of ToList()) consistently. If you must use sync, create a separate DbContext instance.",
    "Use DbContext pooling for high-throughput scenarios: services.AddDbContextPool<MyDbContext>(options => options.UseSqlServer(connectionString), poolSize: 128);"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-03-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}