DapperLib/Dapper · error · InvalidOperationException
If specifying IsFixedLength, a Length must also be specifie
Error message
If specifying IsFixedLength, a Length must also be specified
What it means
DbString.AddParameter throws InvalidOperationException when IsFixedLength is true but Length is still its default of -1. A fixed-length string (CHAR/NCHAR) requires an explicit size so the provider can size the parameter; without it the command would be malformed. The message (with its double space) is a known string in the Dapper source.
Source
Thrown at Dapper/DbString.cs:75
public string? Value { get; set; }
/// <summary>
/// Gets a string representation of this DbString.
/// </summary>
public override string ToString() => Value is null
? $"Dapper.DbString (Value: null, Length: {Length}, IsAnsi: {IsAnsi}, IsFixedLength: {IsFixedLength})"
: $"Dapper.DbString (Value: '{Value}', Length: {Length}, IsAnsi: {IsAnsi}, IsFixedLength: {IsFixedLength})";
/// <summary>
/// Add the parameter to the command... internal use only
/// </summary>
/// <param name="command"></param>
/// <param name="name"></param>
public void AddParameter(IDbCommand command, string name)
{
if (IsFixedLength && Length == -1)
{
throw new InvalidOperationException("If specifying IsFixedLength, a Length must also be specified");
}
bool add = !command.Parameters.Contains(name);
IDbDataParameter param;
if (add)
{
param = command.CreateParameter();
param.ParameterName = name;
}
else
{
param = (IDbDataParameter)command.Parameters[name];
}
#pragma warning disable 0618
param.Value = SqlMapper.SanitizeParameterValue(Value);
#pragma warning restore 0618
if (Length == -1 && Value is not null && Value.Length <= DefaultLength)
{
param.Size = DefaultLength;
View on GitHub (pinned to 72a54c475f)
Solutions
- Set an explicit Length matching the column size whenever IsFixedLength=true (e.g. Length = 50).
- If the column is variable length, leave IsFixedLength=false (default) so Length=-1 is allowed.
- Centralize DbString construction in a helper that asserts IsFixedLength implies a positive Length.
Example fix
// before
var p = new DbString { Value = "abc", IsFixedLength = true };
cnn.Execute("insert into T(C) values (@c)", new { c = p });
// after
var p = new DbString { Value = "abc", IsFixedLength = true, Length = 50 }; Defensive patterns
Strategy: validation
Validate before calling
var p = new DbString { Value = s, IsFixedLength = true };
if (p.IsFixedLength && p.Length <= 0) throw new InvalidOperationException("Fixed-length DbString requires a positive Length");
cnn.Execute(sql, new { c = p }); Try / catch
try { cnn.Execute(sql, new { c = p }); }
catch (InvalidOperationException ex) when (ex.Message.Contains("IsFixedLength"))
{ /* set Length and retry / log config error */ } Prevention
- Always set Length when IsFixedLength=true; treat the pair as inseparable.
- Wrap DbString construction in a helper that asserts the Length invariant.
When it happens
Trigger: Building a DbString, setting IsFixedLength=true, and never assigning Length (it defaults to -1); then passing that DbString as a parameter so AddParameter runs during command setup.
Common situations: Migrating dynamic SQL that used variable-length VARCHAR to fixed-length CHAR without updating parameter setup; copy-pasting a DbString config block and dropping the Length line; reading Length from config where the key was absent.
Related errors
AI-assisted analysis of DapperLib/Dapper@72a54c475f (2026-08-13).
Data as JSON: /api/errors/11df8af384f8e34c.
Report an issue: GitHub.