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
- Add an internal or public parameterless constructor to the type.
- Use Entity<T>().Ctor(...) or [BsonCtor] to define how LiteDB constructs the type, then avoid member-init in the query expression.
- 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
- Add a parameterless constructor to types used in member-init query expressions.
- Avoid member-init syntax for records with primary constructors in queries; project in memory instead.
- Use [BsonCtor] or Entity<T>().Ctor(...) for custom construction logic.
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
- New instance are not supported for {node.Type} when convert
- Constructor for {node.Type.Name} are not supported when conv
- 0
- Extend expression must return an anonymous class to be merge
- connectionString
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/efd0d9f5cac6ae92.
Report an issue: GitHub.