{"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":"exception","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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs#L949-L985","documentation":"Thrown by SetCommandTimeout(TimeSpan) when the supplied timeout is negative (and not Timeout.InfiniteTimeSpan, which maps to 0). Command timeouts must be >= 0 seconds; the underlying ADO.NET DbCommand.CommandTimeout rejects negatives.","triggerScenarios":"Calling db.Database.SetCommandTimeout(negativeTimeSpan) (RelationalDatabaseFacadeExtensions.cs:965-967). Happens when a TimeSpan is computed from a misconfigured setting or arithmetic that underflows.","commonSituations":"Reading timeout from configuration that is unset/default -1; subtracting durations producing a negative; passing -1 seconds expecting infinite (use Timeout.InfiniteTimeSpan instead); misparsed int values.","solutions":["Pass Timeout.InfiniteTimeSpan for no timeout, or a non-negative TimeSpan.","Clamp the configured value to >= 0 before calling SetCommandTimeout: TimeSpan.FromSeconds(Math.Max(0, seconds)).","Validate configuration at startup and fail fast with a clear message instead of at query time."],"exampleFix":"// before\ndb.Database.SetCommandTimeout(TimeSpan.FromSeconds(cfg.CommandTimeout)); // cfg.CommandTimeout = -5\n\n// after\nvar seconds = Math.Max(0, cfg.CommandTimeout);\ndb.Database.SetCommandTimeout(seconds == 0\n    ? Timeout.InfiniteTimeSpan\n    : TimeSpan.FromSeconds(seconds));","handlingStrategy":"validation","validationCode":"if (timeout < TimeSpan.Zero && timeout != Timeout.InfiniteTimeSpan)\n    throw new ArgumentOutOfRangeException(nameof(timeout));\ndb.Database.SetCommandTimeout(timeout);","typeGuard":"static bool IsValidTimeout(TimeSpan t)\n    => t == Timeout.InfiniteTimeSpan || (t >= TimeSpan.Zero && t.TotalSeconds <= int.MaxValue);","tryCatchPattern":null,"preventionTips":["Clamp configuration values to >= 0 before passing.","Use Timeout.InfiniteTimeSpan for no timeout, never a negative span.","Validate timeout settings at startup."],"tags":["efcore","relational","command-timeout","validation","configuration"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}