dotnet/efcore · error · ArgumentException
Invalid type for sequence. Valid types are 'long' (the defau
Error message
Invalid type for sequence. Valid types are 'long' (the default), 'int', 'short', 'byte' and 'decimal'.
What it means
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.
Source
Thrown at src/EFCore.Relational/Metadata/Internal/Sequence.cs:509
{
get => _type ?? DefaultClrType;
set => SetType(value, ConfigurationSource.Explicit);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Type? SetType(Type? type, ConfigurationSource configurationSource)
{
EnsureMutable();
if (type != null
&& !SupportedTypes.Contains(type))
{
throw new ArgumentException(RelationalStrings.BadSequenceType);
}
_type = type;
_typeConfigurationSource = type == null
? null
: configurationSource.Max(_typeConfigurationSource);
return type;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ConfigurationSource? GetTypeConfigurationSource()View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
modelBuilder.HasSequence<string>("OrderSeq"); // string not supported -> throws
// after
modelBuilder.HasSequence<long>("OrderSeq"); Defensive patterns
Strategy: type-guard
Validate before calling
static bool IsSupportedSequenceType(Type t)
=> Microsoft.EntityFrameworkCore.Metadata.Internal.Sequence.SupportedTypes.Contains(t);
// or, against the public surface:
static readonly HashSet<Type> Supported = new() { typeof(byte), typeof(short), typeof(int), typeof(long), typeof(decimal) };
if (!Supported.Contains(typeof(T))) throw new ArgumentException("Unsupported sequence CLR type."); Type guard
static bool IsValidSequenceType<T>() => T switch
{
Type _ when typeof(T) == typeof(byte) => true,
Type _ when typeof(T) == typeof(short) => true,
Type _ when typeof(T) == typeof(int) => true,
Type _ when typeof(T) == typeof(long) => true,
Type _ when typeof(T) == typeof(decimal) => true,
_ => false
}; Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The store type '{storeType}' specified for JSON column '{col
- Unable to deserialize a sequence from model metadata. See in
- Model building is not supported when publishing with NativeA
- Model building is not supported when publishing with NativeA
- The partition key value is of type '{valueType}' which is no
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/257e1a05f2bfc495.
Report an issue: GitHub.