litedb-org/LiteDB · error · LiteException
0
0
Error message
There is no _id field mapped in your type: {member.DataType.FullName} What it means
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.
Source
Thrown at LiteDB/Client/Mapper/BsonMapper.cs:259
/// <summary>
/// Register a property as a DbRef - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
/// </summary>
private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, ITypeNameBinder typeNameBinder)
{
// get entity
var entity = mapper.GetEntityMapper(member.DataType);
member.Serialize = (obj, m) =>
{
// supports null values when "SerializeNullValues = true"
if (obj == null) return BsonValue.Null;
entity.WaitForInitialization();
var idField = entity.Id;
// #768 if using DbRef with interface with no ID mapped
if (idField == null) throw new LiteException(0, "There is no _id field mapped in your type: " + member.DataType.FullName);
var id = idField.Getter(obj);
var bsonDocument = new BsonDocument
{
["$id"] = m.Serialize(id.GetType(), id, 0),
["$ref"] = member.DbRefCollectionName
};
if (member.DataType != obj.GetType())
{
bsonDocument["$type"] = mapper.SerializeTypeName(obj.GetType());
}
return bsonDocument;
};
member.Deserialize = (bson, m) =>View on GitHub (pinned to f906a5f850)
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.
Example fix
// before
public class Post
{
public int Id { get; set; }
[BsonRef] public IAuthor Author { get; set; } // interface, no Id
}
// after
public class Post
{
public int Id { get; set; }
[BsonRef] public Author Author { get; set; }
}
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
} Defensive patterns
Strategy: validation
Validate before calling
// Ensure the referenced type has an Id member before relying on DbRef.
var refMapper = BsonMapper.Global.GetEntityMapper(typeof(TRef));
if (refMapper.Id == null)
throw new InvalidOperationException($"{typeof(TRef)} has no _id mapped; cannot use as DbRef target."); Try / catch
try { db.GetCollection<T>().Insert(entity); }
catch (LiteException ex) when (ex.Message.Contains("no _id field mapped"))
{
// add an Id property/[BsonId] to the referenced type and retry
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- MAPPING_ERROR
- entity
- The type BsonRefId<T> can only be used in LiteDB LINQ expres
- BsonRefId<T> requires a DbRef collection name. Member '{memb
- doc
AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13).
Data as JSON: /api/errors/0de1a7be18f31fe2.
Report an issue: GitHub.