{"record":{"id":"efd0d9f5cac6ae92","repo":"litedb-org/LiteDB","slug":"new-instance-of-node-type-are-not-supported-beca","errorCode":null,"errorMessage":"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\" }}`.","messagePattern":"New instance of (.+?) are not supported because contains ctor with parameter\\. Try use only property initializers: `new (.+?) (.+?)\\}`\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs","lineNumber":395,"sourceCode":"                    _builder.AppendFormat(\"'{0}': \", member.Name);\n                    this.Visit(node.Arguments[i]);\n                }\n\n                _builder.Append(\" }\");\n            }\n\n            return node;\n        }\n\n        /// <summary>\n        /// Visit :: x => `new MyClass { Id = 10 }`\n        /// </summary>\n        protected override Expression VisitMemberInit(MemberInitExpression node)\n        {\n            // works only for empty ctor\n            if (node.NewExpression.Constructor.GetParameters().Length > 0)\n            {\n                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\\\" }}`.\");\n            }\n\n            _builder.Append(\"{\");\n\n            for (var i = 0; i < node.Bindings.Count; i++)\n            {\n                var bind = node.Bindings[i] as MemberAssignment;\n                var member = this.ResolveMember(bind.Member, out var memberMapper);\n\n                _builder.Append(i > 0 ? \", \" : \"\");\n                _builder.Append(member.Substring(1));\n                _builder.Append(\":\");\n\n                if (!TryVisitDbRefIdExpression(bind.Expression, memberMapper))\n                {\n                    this.Visit(bind.Expression);\n                }\n            }","sourceCodeStart":377,"sourceCodeEnd":413,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/Linq/LinqExpressionVisitor.cs#L377-L413","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before -- type has no parameterless ctor\npublic class MyRecord\n{\n    public MyRecord(int id) { Id = id; }\n    public int Id { get; set; }\n    public string Name { get; set; }\n}\n// query using member-init fails\n\n// after -- add parameterless ctor\npublic class MyRecord\n{\n    public MyRecord() {} // added\n    public MyRecord(int id) { Id = id; }\n    public int Id { get; set; }\n    public string Name { get; set; }\n}","handlingStrategy":"validation","validationCode":"// Check that the type used in member-init expressions has a parameterless constructor\nstatic bool HasParameterlessCtor(Type type)\n{\n    return type.GetConstructor(Type.EmptyTypes) != null;\n}","typeGuard":"// Ensure the type has a parameterless constructor\nstatic bool CanUseMemberInit<T>() => typeof(T).GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try\n{\n    // query using new MyType { Prop = value }\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"contains ctor with parameter\"))\n{\n    throw;\n}","preventionTips":["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."],"tags":["linq","member-init","constructor","new-instance"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}