{"record":{"id":"257e1a05f2bfc495","repo":"dotnet/efcore","slug":"invalid-type-for-sequence-valid-types-are-long","errorCode":null,"errorMessage":"Invalid type for sequence. Valid types are 'long' (the default), 'int', 'short', 'byte' and 'decimal'.","messagePattern":"Invalid type for sequence\\. Valid types are 'long' \\(the default\\), 'int', 'short', 'byte' and 'decimal'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/Sequence.cs","lineNumber":509,"sourceCode":"    {\n        get => _type ?? DefaultClrType;\n        set => SetType(value, ConfigurationSource.Explicit);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual Type? SetType(Type? type, ConfigurationSource configurationSource)\n    {\n        EnsureMutable();\n\n        if (type != null\n            && !SupportedTypes.Contains(type))\n        {\n            throw new ArgumentException(RelationalStrings.BadSequenceType);\n        }\n\n        _type = type;\n\n        _typeConfigurationSource = type == null\n            ? null\n            : configurationSource.Max(_typeConfigurationSource);\n\n        return type;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual ConfigurationSource? GetTypeConfigurationSource()","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/Sequence.cs#L491-L527","documentation":"Thrown by Sequence.SetType when the type passed is not in Sequence.SupportedTypes ({byte, long, int, short, decimal}). Database sequences only operate over numeric types, so EF rejects any other CLR type (string, Guid, DateTime, etc.) up front with an ArgumentException.","triggerScenarios":"Sequence.cs:506-510, in SetType(type, configurationSource) called from HasSequence<T>() / modelBuilder.HasSequence(...).HasColumnType(...) or sequence.SetType. Any T or Type outside the five supported numeric types triggers it.","commonSituations":"HasSequence<string>(\"Seq\"); using a key generator expecting Guid/string; copying a sequence config and changing the generic; attempting to back an identity-like column with an unsupported type.","solutions":["Use one of the supported numeric types: byte, short, int, long (default), or decimal.","If you need a different CLR type on the property, keep the sequence as long/int and convert the value via a value converter rather than changing the sequence type.","Remove the HasSequence call entirely if the column uses a database-native auto-increment instead."],"exampleFix":"// before\nmodelBuilder.HasSequence<string>(\"OrderSeq\"); // string not supported -> throws\n\n// after\nmodelBuilder.HasSequence<long>(\"OrderSeq\");","handlingStrategy":"type-guard","validationCode":"static bool IsSupportedSequenceType(Type t)\n    => Microsoft.EntityFrameworkCore.Metadata.Internal.Sequence.SupportedTypes.Contains(t);\n\n// or, against the public surface:\nstatic readonly HashSet<Type> Supported = new() { typeof(byte), typeof(short), typeof(int), typeof(long), typeof(decimal) };\nif (!Supported.Contains(typeof(T))) throw new ArgumentException(\"Unsupported sequence CLR type.\");","typeGuard":"static bool IsValidSequenceType<T>() => T switch\n{\n    Type _ when typeof(T) == typeof(byte)    => true,\n    Type _ when typeof(T) == typeof(short)   => true,\n    Type _ when typeof(T) == typeof(int)     => true,\n    Type _ when typeof(T) == typeof(long)    => true,\n    Type _ when typeof(T) == typeof(decimal) => true,\n    _ => false\n};","tryCatchPattern":null,"preventionTips":["Restrict HasSequence<T> to the five numeric types in a shared helper.","Add a unit test that asserts every HasSequence call uses a supported type."],"tags":["relational","sequence","type-mapping"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}