When insert document causes LiteException.
See original GitHub issueWhen i try to insert document on collection cause LiteException:
LiteDB.LiteException: Invalid BSON data type ‘Null’ on field ‘_id’. at LiteDB.LiteEngine.InsertDocument(CollectionPage col, BsonDocument doc, BsonType autoId) at LiteDB.LiteEngine.<>cDisplayClass22_0.<Insert>b0(CollectionPage col) at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func2 action) at LiteDB.LiteEngine.Insert(String collection, BsonDocument doc, BsonType autoId) at LiteDB.LiteCollection1.Insert(T document) at ForGer.Modules.DeveloperModule.CadastraTodosAsync(CommandContext ctx) in C:\Users\Nathan Ferreira\Desktop\ForGer\ForGer\Modules\DeveloperModule.cs:line 48 at DSharpPlus.CommandsNext.Command.ExecuteAsync(CommandContext ctx)
Class:
public class Usuario
{
[BsonId]
protected ObjectId _id;
[BsonField("user_id")]
private string _user_id;
[BsonIgnore]
public ulong UserId {
get { return ulong.Parse(_user_id); }
set { _user_id = value.ToString(); }
}
[BsonField("experience")]
public long Experience { get; set; }
}
Insert method:
var user = new User();
user.UserId = 235088799074484224;
user.Experience = 62754;
var mapper = new BsonMapper();
mapper.IncludeNonPublic = true; // accepts protected, internal and private fields.
var db = new LiteDatabase("Users.db", mapper);
var col = db.GetCollection<Usuario>();
col.Insert(user); // Causes LiteException Invalid BSON data type 'Null' on field '_id'.
Same exception in this class: (Using no protected fields)
public class Usuario
{
[BsonId]
public ObjectId _id;
[BsonField("user_id")]
public ulong UserId { get; set; }
[BsonField("experience")]
public long Experience { get; set; }
}
No way to auto create an object id?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
@nathan130200 make your _id field a property and it should work, or use the
BsonMapper.Global.IncludeFields = true
optionFixed! Thanks for help.