dotnet/efcore · error · ArgumentException

Invalid type for sequence. Valid types are 'long' (the…

Error message

Invalid type for sequence. Valid types are 'long' (the default), 'int', 'short', 'byte' and 'decimal'.

What it means

Thrown when SetType is called on a Sequence with a CLR type not in the supported set. EF Core sequences are database counters and only the integer/decimal families are valid backing types.

Solutions

  1. Use one of the supported types: long (default), int, short, byte, or decimal.
  2. Switch to long unless a narrower integer range is required.
  3. If decimal precision is needed, use decimal and verify the provider supports decimal sequences (not all do).

Example fix

// before
modelBuilder.HasSequence<Guid>("MySeq"); // throws

// after
modelBuilder.HasSequence<long>("MySeq");
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate the type before assigning it to a sequence.
var supported = new[] { typeof(long), typeof(int), typeof(short), typeof(byte), typeof(decimal) };
if (!supported.Contains(requestedType))
    throw new ArgumentOutOfRangeException(nameof(requestedType),
        "Sequence type must be one of: long, int, short, byte, decimal.");
modelBuilder.HasSequence(requestedType, "OrderSeq");

Type guard

static bool IsValidSequenceType(Type t)
    => t == typeof(long) || t == typeof(int) || t == typeof(short)
       || t == typeof(byte) || t == typeof(decimal);

Prevention

When it happens

Trigger: Sequence.SetType(type, configurationSource) when type is non-null and not in SupportedTypes (byte, short, int, long, decimal). Triggered by HasSequence<SomeType>() or sequence.HasType(typeof(...)) with an unsupported type such as Guid, string, DateTime, or float.

Common situations: Calling HasSequence<Guid>() by mistake; passing a float/double wanting fractional increments; copying a generic argument from the column type into the sequence; using a custom value type.

Related errors


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

Appendix: 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 3a2006ef56)