{"record":{"id":"885306632baf1e75","repo":"dotnet/efcore","slug":"the-specified-maxbatchsize-value-value-is-no","errorCode":null,"errorMessage":"The specified 'MaxBatchSize' value '{value}' is not valid. It must be a positive number.","messagePattern":"The specified 'MaxBatchSize' value '(.+?)' is not valid\\. It must be a positive number\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalOptionsExtension.cs","lineNumber":197,"sourceCode":"    /// <summary>\n    ///     The maximum number of statements that will be included in commands sent to the database\n    ///     during <see cref=\"DbContext.SaveChanges()\" /> or <see langword=\"null\" /> if none has been set.\n    /// </summary>\n    public virtual int? MaxBatchSize\n        => _maxBatchSize;\n\n    /// <summary>\n    ///     Creates a new instance with all options the same as for this instance, but with the given option changed.\n    ///     It is unusual to call this method directly. Instead use <see cref=\"DbContextOptionsBuilder\" />.\n    /// </summary>\n    /// <param name=\"maxBatchSize\">The option to change.</param>\n    /// <returns>A new instance with the option changed.</returns>\n    public virtual RelationalOptionsExtension WithMaxBatchSize(int? maxBatchSize)\n    {\n        if (maxBatchSize.HasValue\n            && maxBatchSize <= 0)\n        {\n            throw new InvalidOperationException(RelationalStrings.InvalidMaxBatchSize(maxBatchSize));\n        }\n\n        var clone = Clone();\n\n        clone._maxBatchSize = maxBatchSize;\n\n        return clone;\n    }\n\n    /// <summary>\n    ///     The minimum number of statements that are needed for a multi-statement command sent to the database\n    ///     during <see cref=\"DbContext.SaveChanges()\" /> or <see langword=\"null\" /> if none has been set.\n    /// </summary>\n    public virtual int? MinBatchSize\n        => _minBatchSize;\n\n    /// <summary>\n    ///     Creates a new instance with all options the same as for this instance, but with the given option changed.","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalOptionsExtension.cs#L179-L215","documentation":"Thrown by RelationalOptionsExtension.WithMaxBatchSize(int?) at lines 194-197 when the supplied value is <= 0. MaxBatchSize controls how many INSERT/UPDATE/DELETE statements EF groups into a single round-trip during SaveChanges; it must be at least 1 to be meaningful.","triggerScenarios":"Calling optionsBuilder.UseSqlServer(connStr, sql => sql.MaxBatchSize(0)) or any negative value. Also occurs when the value comes from a config source that is misconfigured or left at a default sentinel like 0.","commonSituations":"Reading MaxBatchSize from appsettings with a missing or zero default. Setting it to 0 intending to 'disable batching' (which is actually done by setting it to 1).","solutions":["Set MaxBatchSize to a positive integer (e.g., 10, 42) representing the max statements per batch.","To effectively disable batching, set MaxBatchSize to 1.","Validate config: if (configuredMaxBatchSize > 0) sql.MaxBatchSize(configuredMaxBatchSize);"],"exampleFix":"// before\noptionsBuilder.UseSqlServer(connStr, sql => sql.MaxBatchSize(0));\n\n// after: positive value, or 1 to disable batching\noptionsBuilder.UseSqlServer(connStr, sql => sql.MaxBatchSize(1));","handlingStrategy":"validation","validationCode":"var maxBatch = configuration.GetValue<int?>(\"MaxBatchSize\");\nif (maxBatch.HasValue && maxBatch <= 0) throw new ArgumentOutOfRangeException(nameof(maxBatch), \"MaxBatchSize must be > 0\");\noptionsBuilder.UseSqlServer(connStr, sql => { if (maxBatch > 0) sql.MaxBatchSize(maxBatch.Value); });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate batch size config values are > 0 before passing to EF.","Treat 0 as 'unset' rather than passing it through.","Document that MaxBatchSize=1 disables batching."],"tags":["batch-size","configuration","argument-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}