litedb-org/LiteDB · error · NotSupportedException

Expression {expr} not supported for BsonRefId<T>.

Error message

Expression {expr} not supported for BsonRefId<T>.

What it means

Thrown when translating an array initializer for a DbRef array property (e.g., Product[] Products) where one of the array elements is not a valid BsonRefId<T> expression. The visitor recursed into TryVisitDbRefIdExpression for that element and it returned false, meaning the expression shape was unrecognized.

Source

Thrown at LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs:794

                    return false;

                // new T[] { new BsonRefId<T>, ... }
                case NewArrayExpression expr
                    when !isInList && expr.Type.IsArray && memberMapper.UnderlyingType.IsAssignableFrom(expr.Type.GetElementType()):

                    _builder.Append("[ ");

                    for (var i = 0; i < expr.Expressions.Count; i++)
                    {
                        if (i > 0)
                        {
                            _builder.Append(", ");
                        }

                        if (!TryVisitDbRefIdExpression(expr.Expressions[i], memberMapper, true))
                        {
                            throw new NotSupportedException($"Expression {expr} not supported for BsonRefId<T>.");
                        }
                    }

                    _builder.Append(" ]");
                    return true;

                // new List<T> { new BsonRefId<T>, ... }
                case ListInitExpression { Type: { IsConstructedGenericType: true, GenericTypeArguments.Length: 1 } } expr
                    when !isInList && expr.Type.GetGenericTypeDefinition() == typeof(List<>) && memberMapper.UnderlyingType.IsAssignableFrom(expr.Type.GetGenericArguments()[0]):

                    _builder.Append("[ ");

                    for (var i = 0; i < expr.Initializers.Count; i++)
                    {
                        if (i > 0)
                        {
                            _builder.Append(", ");
                        }

View on GitHub (pinned to f906a5f850)

Solutions

  1. Wrap every array element in BsonRefId<T>: Products = new[] { new BsonRefId<Product>(id1), new BsonRefId<Product>(id2) }.
  2. Ensure the BsonRefId generic argument matches (or is assignable to) the array element type.
  3. Do not mix raw entity instances with BsonRefId<T> in the same initializer.

Example fix

// before
x => new Order
{
    Products = new[] { existingProduct }
}
// after
x => new Order
{
    Products = new[] { new BsonRefId<Product>(existingProduct.Id) }
}
Defensive patterns

Strategy: validation

Validate before calling

// Ensure all array elements in a DbRef update expression are BsonRefId<T>
// No runtime API; validate at code-review time that each element uses new BsonRefId<T>(id)
// e.g.: Products = new[] { new BsonRefId<Product>(p1Id), new BsonRefId<Product>(p2Id) }

Try / catch

try
{
    col.UpdateMany(
        x => new Order { Products = new[] { new BsonRefId<Product>(id1), new BsonRefId<Product>(id2) } },
        x => x.Id == orderId);
}
catch (NotSupportedException ex) when (ex.Message.Contains("not supported for BsonRefId"))
{
    // Review the array initializer: every element must be new BsonRefId<T>(id)
    throw;
}

Prevention

When it happens

Trigger: Writing a LINQ UpdateMany/Insert expression like x => new Order { Products = new[] { someProductInstance } } where the array element is a direct entity reference or an unsupported expression instead of new BsonRefId<Product>(id).

Common situations: Forgetting to wrap array element IDs in BsonRefId<T> when doing bulk reference updates. Mixing actual entity instances with BsonRefId<T> in the same array initializer. Using new BsonRefId<WrongType>(id) where the type is not assignable to the array element type.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/d03b9b8fcd58f244. Report an issue: GitHub.