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

  1. 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).
  2. Restore the sequence annotation string from source control if a merge corrupted it.
  3. Delete the corrupt sequence entry and re-add the sequence via HasSequence in OnModelCreating, then regenerate the snapshot.
  4. 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

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


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)