{"record":{"id":"0de1a7be18f31fe2","repo":"litedb-org/LiteDB","slug":"0-0de1a7","errorCode":"0","errorMessage":"There is no _id field mapped in your type: {member.DataType.FullName}","messagePattern":"There is no _id field mapped in your type: (.+?)","errorType":"exception","errorClass":"LiteException","httpStatus":null,"severity":"error","filePath":"LiteDB/Client/Mapper/BsonMapper.cs","lineNumber":259,"sourceCode":"\n        /// <summary>\n        /// Register a property as a DbRef - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only\n        /// </summary>\n        private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, ITypeNameBinder typeNameBinder)\n        {\n            // get entity\n            var entity = mapper.GetEntityMapper(member.DataType);\n            \n            member.Serialize = (obj, m) =>\n            {\n                // supports null values when \"SerializeNullValues = true\"\n                if (obj == null) return BsonValue.Null;\n                entity.WaitForInitialization();\n                \n                var idField = entity.Id;\n\n                // #768 if using DbRef with interface with no ID mapped\n                if (idField == null) throw new LiteException(0, \"There is no _id field mapped in your type: \" + member.DataType.FullName);\n\n                var id = idField.Getter(obj);\n\n                var bsonDocument = new BsonDocument\n                {\n                    [\"$id\"] = m.Serialize(id.GetType(), id, 0),\n                    [\"$ref\"] = member.DbRefCollectionName\n                };\n\n                if (member.DataType != obj.GetType())\n                {\n                    bsonDocument[\"$type\"] = mapper.SerializeTypeName(obj.GetType());\n                }\n\n                return bsonDocument;\n            };\n\n            member.Deserialize = (bson, m) =>","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Client/Mapper/BsonMapper.cs#L241-L277","documentation":"Thrown during DbRef serialization (BsonMapper.cs, the RegisterDbRef serialize closure) when the referenced entity type has no _id member mapped. When serializing a DbRef, LiteDB stores only the foreign key ($id) and collection ($ref); it obtains the id by reading the referenced type's Id member. If none is mapped (e.g. the type is an interface, or has no Id/Id-like property and no [BsonId]), this fires with code 0. This is issue #768.","triggerScenarios":"Declaring a [BsonRef] property whose declared type is an interface or abstract base with no Id member, then Inserting/Updating a document whose DbRef serializer runs. Also triggered when the referenced type's Id property was ignored or not mapped.","commonSituations":"Modeling a reference as an interface (e.g. public IAuthor Author) without a concrete mapped type, removing the Id property from a referenced entity, or applying [BsonIgnore] to the Id of a DbRef target.","solutions":["Declare the DbRef property as a concrete class that has an Id member (or Id-named property).","Add [BsonId] to the property that should serve as the key on the referenced type.","Use EntityBuilder.DbRef and EntityBuilder.Id to explicitly map the Id on the referenced type."],"exampleFix":"// before\npublic class Post\n{\n    public int Id { get; set; }\n    [BsonRef] public IAuthor Author { get; set; } // interface, no Id\n}\n\n// after\npublic class Post\n{\n    public int Id { get; set; }\n    [BsonRef] public Author Author { get; set; }\n}\npublic class Author\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n}","handlingStrategy":"validation","validationCode":"// Ensure the referenced type has an Id member before relying on DbRef.\nvar refMapper = BsonMapper.Global.GetEntityMapper(typeof(TRef));\nif (refMapper.Id == null)\n    throw new InvalidOperationException($\"{typeof(TRef)} has no _id mapped; cannot use as DbRef target.\");","typeGuard":null,"tryCatchPattern":"try { db.GetCollection<T>().Insert(entity); }\ncatch (LiteException ex) when (ex.Message.Contains(\"no _id field mapped\"))\n{\n    // add an Id property/[BsonId] to the referenced type and retry\n}","preventionTips":["Declare DbRef properties as concrete classes with an Id member.","Add [BsonId] explicitly when the Id convention does not match.","Avoid interfaces/abstract bases as DbRef property types unless mapped."],"tags":["mapper","dbref","entity","id-mapping"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}