dotnet/efcore · error · ArgumentException

Unable to deserialize a sequence from model metadata. See in

Error message

Unable to deserialize a sequence from model metadata. See inner exception for details.

What it means

Thrown by SequenceData.Deserialize when parsing the serialized sequence annotation (used by the model snapshot / migration pipeline) fails — any exception during extraction of name, schema, start value, increment, min/max, type, or cyclic flag is wrapped in this ArgumentException with the original as InnerException. It signals a corrupt or malformed sequence string stored in a snapshot/migration.

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 dbf9771522)

Solutions

  1. Regenerate the model snapshot and migrations (dotnet ef migrations remove / add) so the sequence annotation is written by the current EF version.
  2. Inspect the InnerException for the exact parse failure (IndexOutOfRange, FormatException) and fix the offending field in the snapshot.
  3. Avoid hand-editing sequence annotations; manage sequences only through HasSequence in OnModelCreating.
  4. If migrating across major EF versions, recreate the snapshot rather than carrying the old one forward.

Example fix

// before - snapshot contains a hand-edited/truncated sequence string
// model["Relational:Sequences"] = "'OrderSeq'..." (malformed) -> throws on load

// after - regenerate the snapshot with the current tooling
// dotnet ef migrations remove
// dotnet ef migrations add RecreateSequences
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate the snapshot file is not hand-edited before loading the model
// (best prevention: regenerate via `dotnet ef migrations remove` / `add`)

Try / catch

try
{
    // operation that loads/uses the model snapshot containing the sequence annotation
}
catch (ArgumentException ex) when (ex.Message.Contains("Unable to deserialize a sequence"))
{
    // The snapshot/migration sequence annotation is corrupt.
    // Surface a clear instruction to regenerate the snapshot.
    throw new InvalidOperationException(
        "Model snapshot sequence data is corrupt or from an incompatible EF version. " +
        "Regenerate migrations: dotnet ef migrations remove && dotnet ef migrations add <Name>", ex);
}

Prevention

When it happens

Trigger: Sequence.cs:707-731, invoked via the obsolete Sequence(model, annotationName) constructor used by the snapshot model processor (see issue #18557). Triggered when a migration/snapshot's Relational:Sequences annotation has been hand-edited, truncated, or produced by an incompatible EF version so ExtractValue/AsLong/AsType/AsBool throw.

Common situations: Manually editing a migration or ModelSnapshot; merging conflicts in a snapshot file that left malformed sequence data; upgrading EF Core across versions where the sequence serialization format changed; a snapshot generated by one EF version consumed by another.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/36dda8a9d77a8696. Report an issue: GitHub.