{"record":{"id":"36dda8a9d77a8696","repo":"dotnet/efcore","slug":"unable-to-deserialize-a-sequence-from-model-metada","errorCode":null,"errorMessage":"Unable to deserialize a sequence from model metadata. See inner exception for details.","messagePattern":"Unable to deserialize a sequence from model metadata\\. See inner exception for details\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/Sequence.cs","lineNumber":729,"sourceCode":"                var data = new SequenceData();\n\n                // ReSharper disable PossibleInvalidOperationException\n                var position = 0;\n                data.Name = ExtractValue(value, ref position)!;\n                data.Schema = ExtractValue(value, ref position);\n                data.StartValue = (long)AsLong(ExtractValue(value, ref position)!)!;\n                data.IncrementBy = (int)AsLong(ExtractValue(value, ref position)!)!;\n                data.MinValue = AsLong(ExtractValue(value, ref position));\n                data.MaxValue = AsLong(ExtractValue(value, ref position));\n                data.ClrType = AsType(ExtractValue(value, ref position)!);\n                data.IsCyclic = AsBool(ExtractValue(value, ref position));\n                // ReSharper restore PossibleInvalidOperationException\n\n                return data;\n            }\n            catch (Exception ex)\n            {\n                throw new ArgumentException(RelationalStrings.BadSequenceString, ex);\n            }\n        }\n\n        private static string? ExtractValue(string value, ref int position)\n        {\n            position = value.IndexOf('\\'', position) + 1;\n\n            var end = value.IndexOf('\\'', position);\n\n            while (end + 1 < value.Length\n                   && value[end + 1] == '\\'')\n            {\n                end = value.IndexOf('\\'', end + 2);\n            }\n\n            var extracted = value[position..end].Replace(\"''\", \"'\");\n            position = end + 1;\n","sourceCodeStart":711,"sourceCodeEnd":747,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/Sequence.cs#L711-L747","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Regenerate the model snapshot and migrations (dotnet ef migrations remove / add) so the sequence annotation is written by the current EF version.","Inspect the InnerException for the exact parse failure (IndexOutOfRange, FormatException) and fix the offending field in the snapshot.","Avoid hand-editing sequence annotations; manage sequences only through HasSequence in OnModelCreating.","If migrating across major EF versions, recreate the snapshot rather than carrying the old one forward."],"exampleFix":"// before - snapshot contains a hand-edited/truncated sequence string\n// model[\"Relational:Sequences\"] = \"'OrderSeq'...\" (malformed) -> throws on load\n\n// after - regenerate the snapshot with the current tooling\n// dotnet ef migrations remove\n// dotnet ef migrations add RecreateSequences","handlingStrategy":"try-catch","validationCode":"// Validate the snapshot file is not hand-edited before loading the model\n// (best prevention: regenerate via `dotnet ef migrations remove` / `add`)","typeGuard":null,"tryCatchPattern":"try\n{\n    // operation that loads/uses the model snapshot containing the sequence annotation\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Unable to deserialize a sequence\"))\n{\n    // The snapshot/migration sequence annotation is corrupt.\n    // Surface a clear instruction to regenerate the snapshot.\n    throw new InvalidOperationException(\n        \"Model snapshot sequence data is corrupt or from an incompatible EF version. \" +\n        \"Regenerate migrations: dotnet ef migrations remove && dotnet ef migrations add <Name>\", ex);\n}","preventionTips":["Never hand-edit sequence annotations in ModelSnapshot or migrations.","After EF Core upgrades, regenerate the snapshot rather than carrying the old one.","Resolve merge conflicts in snapshot files by regenerating, not by manual splicing."],"tags":["relational","sequence","migrations","snapshot","deserialization"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}