dotnet/efcore · error · ArgumentException
Unable to deserialize a sequence from model metadata. See…
Error message
Unable to deserialize a sequence from model metadata. See inner exception for details.
What it means
Thrown when SequenceData.Deserialize cannot parse a serialized sequence string from model metadata. The serialized form is a positional, single-quote-delimited tuple; any deviation in shape or content trips the inner exception which is wrapped as BadSequenceString.
Solutions
- Re-scaffold the model snapshot so the sequence annotation is regenerated correctly (Add-Migration / dotnet ef migrations add with no changes to force a rebuild).
- Restore the sequence annotation string from source control if a merge corrupted it.
- Delete the corrupt sequence entry and re-add the sequence via HasSequence in OnModelCreating, then regenerate the snapshot.
- Upgrade EF Core on all dev machines to the same version so serialization format matches.
Example fix
// before: corrupt annotation in ModelSnapshot
modelBuilder.HasAnnotation("Relational:Sequence:OrderSeq", "'OrderSeq''dbo'1Xbad'");
// after: regenerate the snapshot, or restore the well-formed annotation
modelBuilder.HasAnnotation("Relational:Sequence:OrderSeq", "'OrderSeq''dbo'110019223372036854775807System.Int64'False'"); Defensive patterns
Strategy: try-catch
Try / catch
try
{
var seq = model.FindSequence(name); // triggers Deserialize if annotation is lazy
}
catch (ArgumentException ex) when (ex.Message.Contains("Unable to deserialize a sequence"))
{
// The snapshot annotation is corrupt. Re-scaffold or restore from source control.
logger.LogError(ex, "Corrupt sequence annotation in model snapshot.");
throw;
} Prevention
- Never hand-edit the serialized sequence annotation in the ModelSnapshot.
- Keep the ModelSnapshot under source control and resolve merge conflicts carefully.
- Use the same EF Core version across all developers and CI to avoid format drift.
- After a version upgrade, regenerate the snapshot with a no-op migration.
When it happens
Trigger: Sequence.SequenceData.Deserialize(value) catching any exception from ExtractValue/AsLong/AsType/AsBool during the fixed-order parse (Name, Schema, StartValue, IncrementBy, MinValue, MaxValue, ClrType, IsCyclic). Fires when the stored annotation string for a sequence is corrupt or hand-edited.
Common situations: Manually editing the Migrations/ModelSnapshot annotation string for a sequence; cross-version model snapshot incompatibility where the serialization format changed; truncated snapshot files; merging conflicts that mangled the sequence annotation.
Related errors
- Invalid type for sequence. Valid types are 'long' (the…
- The object ' ' has been removed from the model.
- The foreign keys on ' ' and on ' ' are both mapped to ' …
- The foreign keys on ' ' and on ' ' are both mapped to ' …
- The foreign keys on ' ' and on ' ' are both mapped to ' …
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/36dda8a9d77a8696.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Metadata/Internal/Sequence.cs:729
var data = new SequenceData();
// ReSharper disable PossibleInvalidOperationException
var position = 0;
data.Name = ExtractValue(value, ref position)!;
data.Schema = ExtractValue(value, ref position);
data.StartValue = (long)AsLong(ExtractValue(value, ref position)!)!;
data.IncrementBy = (int)AsLong(ExtractValue(value, ref position)!)!;
data.MinValue = AsLong(ExtractValue(value, ref position));
data.MaxValue = AsLong(ExtractValue(value, ref position));
data.ClrType = AsType(ExtractValue(value, ref position)!);
data.IsCyclic = AsBool(ExtractValue(value, ref position));
// ReSharper restore PossibleInvalidOperationException
return data;
}
catch (Exception ex)
{
throw new ArgumentException(RelationalStrings.BadSequenceString, ex);
}
}
private static string? ExtractValue(string value, ref int position)
{
position = value.IndexOf('\'', position) + 1;
var end = value.IndexOf('\'', position);
while (end + 1 < value.Length
&& value[end + 1] == '\'')
{
end = value.IndexOf('\'', end + 2);
}
var extracted = value[position..end].Replace("''", "'");
position = end + 1;
View on GitHub (pinned to 3a2006ef56)