litedb-org/LiteDB · error · NotSupportedException

New instance of {node.Type} are not supported because contai

Error message

New instance of {node.Type} are not supported because contains ctor with parameter. Try use only property initializers: `new {node.Type.Name} {{ PropA = 1, PropB == "John" }}`.

What it means

Thrown by VisitMemberInit when a new SomeType { Prop = value } expression is used but the type's constructor has parameters (i.e., there is no parameterless constructor). VisitMemberInit only supports types with a parameterless constructor because it emits a BsonExpression document literal { field: value, ... } without constructor arguments.

Source

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

                    _builder.AppendFormat("'{0}': ", member.Name);
                    this.Visit(node.Arguments[i]);
                }

                _builder.Append(" }");
            }

            return node;
        }

        /// <summary>
        /// Visit :: x => `new MyClass { Id = 10 }`
        /// </summary>
        protected override Expression VisitMemberInit(MemberInitExpression node)
        {
            // works only for empty ctor
            if (node.NewExpression.Constructor.GetParameters().Length > 0)
            {
                throw new NotSupportedException($"New instance of {node.Type} are not supported because contains ctor with parameter. Try use only property initializers: `new {node.Type.Name} {{ PropA = 1, PropB == \"John\" }}`.");
            }

            _builder.Append("{");

            for (var i = 0; i < node.Bindings.Count; i++)
            {
                var bind = node.Bindings[i] as MemberAssignment;
                var member = this.ResolveMember(bind.Member, out var memberMapper);

                _builder.Append(i > 0 ? ", " : "");
                _builder.Append(member.Substring(1));
                _builder.Append(":");

                if (!TryVisitDbRefIdExpression(bind.Expression, memberMapper))
                {
                    this.Visit(bind.Expression);
                }
            }

View on GitHub (pinned to f906a5f850)

Solutions

  1. Add an internal or public parameterless constructor to the type.
  2. Use Entity<T>().Ctor(...) or [BsonCtor] to define how LiteDB constructs the type, then avoid member-init in the query expression.
  3. Project in memory after fetching rather than using new-with-initializer inside the query.

Example fix

// before -- type has no parameterless ctor
public class MyRecord
{
    public MyRecord(int id) { Id = id; }
    public int Id { get; set; }
    public string Name { get; set; }
}
// query using member-init fails

// after -- add parameterless ctor
public class MyRecord
{
    public MyRecord() {} // added
    public MyRecord(int id) { Id = id; }
    public int Id { get; set; }
    public string Name { get; set; }
}
Defensive patterns

Strategy: validation

Validate before calling

// Check that the type used in member-init expressions has a parameterless constructor
static bool HasParameterlessCtor(Type type)
{
    return type.GetConstructor(Type.EmptyTypes) != null;
}

Type guard

// Ensure the type has a parameterless constructor
static bool CanUseMemberInit<T>() => typeof(T).GetConstructor(Type.EmptyTypes) != null;

Try / catch

try
{
    // query using new MyType { Prop = value }
}
catch (NotSupportedException ex) when (ex.Message.Contains("contains ctor with parameter"))
{
    throw;
}

Prevention

When it happens

Trigger: Using new SomeType { Prop = value } in a LINQ query where SomeType has no parameterless constructor (all constructors take parameters). Common with C# records using primary constructors or classes with only parameterized constructors.

Common situations: Using C# records with primary constructors in upsert/projection expressions. Using immutable types (all-args constructor, no default ctor) in member-init expressions within queries.

Related errors


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