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

  1. Use one of the supported numeric types: byte, short, int, long (default), or decimal.
  2. 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.
  3. 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

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


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