question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] NullReferenceException on Insert with auto id and BsonDocument with `_id=0`

See original GitHub issue

NullReferenceException on Insert with auto id and BsonDocument with _id=0

Version

LiteDB 5.0.2 and the latest from repo, Windows 10, net45

Describe the bug

  1. Given a LiteCollection<BsonDocument> with auto id Int32.
  2. Insert a BsonDocument with _id = 0.
  3. -> NullReferenceException

Code to Reproduce

using (var db = new LiteDatabase(":memory:"))
{
    var test = db.GetCollection("Test", BsonAutoId.Int32);
    var doc = new BsonDocument() { ["_id"] = 0, ["p1"] = 1 };
    test.Insert(doc); // -> NullReferenceException
}

Expected behavior

This scenario works without exceptions and maybe sets the _id in the input document to the generated value.

But if for some reason this scenario is not valid then the exception should be different, some clear error.

Stack

>   LiteDB.dll!LiteDB.LiteCollection<LiteDB.BsonDocument>.Insert(LiteDB.BsonDocument entity) Line 27    C#
    MyLiteDB.exe!TryLiteDB.Program.Main(string[] args) Line 17  C#

Failed code due to null _id:

// checks if must update _id value in entity
if (removed)
{
    _id.Setter(entity, id.RawValue);
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
mbdavidcommented, Feb 19, 2020

I will back into this issue before new release

1reaction
mbdavidcommented, Feb 17, 2020

Thanks again @nightroman, this 3th-party fine tests are great for LiteDB

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB id remains null after InsertOneAsync
But when creating a new instance of the derived class and using InsertOneAsync to add it to my collection, the document is added...
Read more >
C# Driver updating BsonDocument
Hello i got an issue with MongoDB model builder like example i have UserModel for adding or updating existing document in mongo
Read more >
litedb insert的推薦與評價,GITHUB - 最新趨勢觀測站
Version LiteDB v5.0.10 .NET Standard 2.1 Xamarin.Forms v5.0.0.2012. Describe the bug The database seems to get corrupted when I insert data. ... <看更多> ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found