diff --git a/src/SqlPersistence/Config/SqlDialect_MsSqlServer.cs b/src/SqlPersistence/Config/SqlDialect_MsSqlServer.cs index efd960bd5..4c1b53e08 100644 --- a/src/SqlPersistence/Config/SqlDialect_MsSqlServer.cs +++ b/src/SqlPersistence/Config/SqlDialect_MsSqlServer.cs @@ -34,7 +34,18 @@ internal override void SetParameterValue(DbParameter parameter, object value) if (value is ArraySegment charSegment) { parameter.Value = charSegment.Array; - parameter.Size = charSegment.Count; + + // Set to 4000 or -1 to improve query execution plan reuse + // Must be set when exceeding 4000 characters for nvarchar(max) https://stackoverflow.com/a/973269/199551 + parameter.Size = charSegment.Count > 4000 ? -1 : 4000; + } + else if (value is string stringValue) + { + parameter.Value = stringValue; + + // Set to 4000 or -1 to improve query execution plan reuse + // Must be set when exceeding 4000 characters for nvarchar(max) https://stackoverflow.com/a/973269/199551 + parameter.Size = stringValue.Length > 4000 ? -1 : 4000; } else {