{"record":{"id":"ecaa5c0887505dc8","repo":"dotnet/efcore","slug":"timeout-must-be-greater-than-or-equal-to-zero-pro","errorCode":null,"errorMessage":"Timeout must be greater than or equal to zero. Provided timeout: {seconds} seconds.","messagePattern":"Timeout must be greater than or equal to zero\\. Provided timeout: (.+?) seconds\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs","lineNumber":967,"sourceCode":"    ///         <see cref=\"SetCommandTimeout(DatabaseFacade,int?)\" />.\n    ///     </para>\n    ///     <para>\n    ///         See <see href=\"https://aka.ms/efcore-docs-connections\">Connections and connection strings</see> for more information and examples.\n    ///     </para>\n    /// </remarks>\n    /// <param name=\"databaseFacade\">The <see cref=\"DatabaseFacade\" /> for the context.</param>\n    /// <param name=\"timeout\">The timeout to use.</param>\n    public static void SetCommandTimeout(this DatabaseFacade databaseFacade, TimeSpan timeout)\n    {\n        if (timeout == Timeout.InfiniteTimeSpan)\n        {\n            databaseFacade.SetCommandTimeout(0);\n            return;\n        }\n\n        if (timeout < TimeSpan.Zero)\n        {\n            throw new ArgumentException(RelationalStrings.TimeoutTooSmall(timeout.TotalSeconds));\n        }\n\n        if (timeout.TotalSeconds > int.MaxValue)\n        {\n            throw new ArgumentException(RelationalStrings.TimeoutTooBig(timeout.TotalSeconds));\n        }\n\n        databaseFacade.SetCommandTimeout(Convert.ToInt32(timeout.TotalSeconds));\n    }\n\n    /// <summary>\n    ///     Returns the timeout (in seconds) set for commands executed with this <see cref=\"DbContext\" />.\n    /// </summary>\n    /// <remarks>\n    ///     <para>\n    ///         Note that the command timeout is distinct from the connection timeout, which is commonly\n    ///         set on the database connection string.\n    ///     </para>","sourceCodeStart":949,"sourceCodeEnd":985,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs#L949-L985","documentation":"Thrown by RelationalDatabaseFacadeExtensions.SetCommandTimeout(TimeSpan) when the supplied timeout is a negative TimeSpan (and is not Timeout.InfiniteTimeSpan, which is mapped to 0). Command timeouts must be zero or positive; a negative value is treated as a programming error.","triggerScenarios":"Calling Database.SetCommandTimeout(TimeSpan) with a value less than TimeSpan.Zero, e.g. TimeSpan.FromSeconds(-1), or a computed/overflowed negative duration.","commonSituations":"Deriving the timeout from configuration that parses to a negative number; subtracting durations to compute a remaining-time timeout that underflows; default/uninitialized TimeSpan fields passed by mistake.","solutions":["Clamp the value to >= TimeSpan.Zero before calling (use Timeout.InfiniteTimeSpan for no limit, which maps to 0).","Validate configuration: ensure timeout settings parse to a non-negative number.","Pass an int seconds value via the SetCommandTimeout(int) overload after bounds-checking."],"exampleFix":"// before\ncontext.Database.SetCommandTimeout(TimeSpan.FromSeconds(cfg.Timeout)); // cfg.Timeout == -5 -> throws\n\n// after\nvar ts = cfg.Timeout <= 0\n    ? Timeout.InfiniteTimeSpan\n    : TimeSpan.FromSeconds(cfg.Timeout);\ncontext.Database.SetCommandTimeout(ts);","handlingStrategy":"validation","validationCode":"// Clamp negative timeouts before calling SetCommandTimeout\nTimeSpan ts = configuredTimeout;\nif (ts == Timeout.InfiniteTimeSpan)\n    context.Database.SetCommandTimeout(0);\nelse if (ts < TimeSpan.Zero)\n    throw new ArgumentOutOfRangeException(nameof(configuredTimeout), \"Timeout must be >= 0 or InfiniteTimeSpan.\");\nelse if (ts.TotalSeconds > int.MaxValue)\n    context.Database.SetCommandTimeout(int.MaxValue);\nelse\n    context.Database.SetCommandTimeout(ts);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate timeout config parses to a non-negative value.","Use Timeout.InfiniteTimeSpan to mean 'no limit' (mapped to 0).","Default uninitialized timeout fields to a sane positive value."],"tags":["ef-core","relational","command-timeout","validation","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}