{
  "id": "dotnet/aspnet-core-options-validation-failure",
  "signature": "Microsoft.Extensions.Options.OptionsValidationException: A configuration validation error occurred. DataAnnotation validation failed for members: 'ConnectionString' with error: 'The ConnectionString field is required.'",
  "signature_zh": "Microsoft.Extensions.Options.OptionsValidationException: 发生配置验证错误。成员 'ConnectionString' 的数据注释验证失败，错误信息：'ConnectionString 字段是必填的。'",
  "regex": "OptionsValidationException: A configuration validation error occurred\\. DataAnnotation validation failed for members: '.*' with error: '.*'",
  "domain": "dotnet",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "Options validation fails when an application startup validates configuration objects via DataAnnotations and a required property is missing or invalid in appsettings.json or environment variables.",
  "root_cause_type": "generic",
  "root_cause_zh": "当应用程序启动通过 DataAnnotations 验证配置对象且 appsettings.json 或环境变量中缺少必需属性或属性无效时，选项验证失败。",
  "versions": [
    {
      "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": "Microsoft.Extensions.Options.DataAnnotations 7.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Microsoft.Extensions.Options.DataAnnotations 8.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Removing the 'ValidateDataAnnotations()' call in services configuration suppresses the error but bypasses safety checks, potentially causing runtime issues.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Adding a default value to the property in the options class may hide the error if the default is invalid, leading to silent failures.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Only checking appsettings.json while the value is overridden by environment variables with empty string can still trigger validation failure.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the required configuration key exists in appsettings.json or environment variables. For example, add to appsettings.json: '\"MyOptions\": { \"ConnectionString\": \"Server=myServer;Database=myDB;\" }' or set environment variable 'MyOptions__ConnectionString' to a valid value.",
      "success_rate": 0.9,
      "how": "Ensure the required configuration key exists in appsettings.json or environment variables. For example, add to appsettings.json: '\"MyOptions\": { \"ConnectionString\": \"Server=myServer;Database=myDB;\" }' or set environment variable 'MyOptions__ConnectionString' to a valid value.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the property is intentionally optional, remove the '[Required]' attribute from the options class and handle null values in code, or provide a default value using 'options.DefaultValue = \"...\"' in the configuration.",
      "success_rate": 0.85,
      "how": "If the property is intentionally optional, remove the '[Required]' attribute from the options class and handle null values in code, or provide a default value using 'options.DefaultValue = \"...\"' in the configuration.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "确保 appsettings.json 或环境变量中存在所需的配置键。例如，在 appsettings.json 中添加：'\"MyOptions\": { \"ConnectionString\": \"Server=myServer;Database=myDB;\" }' 或设置环境变量 'MyOptions__ConnectionString' 为有效值。",
    "如果该属性是可选的，请从选项类中移除 '[Required]' 属性并在代码中处理 null 值，或在配置中使用 'options.DefaultValue = \"...\"' 提供默认值。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-8.0#options-validation",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-09-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}