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.

LiteDB.LiteException: 'Cannot insert duplicate key in unique index '_id'

See original GitHub issue

How come that i get a LiteDB.LiteException: 'Cannot insert duplicate key in unique index '_id'. The duplicate value is '0'.' with the following code?

It looks like the AutoId doesn’t work in this scenario, or am i missing something?

    public class Test {
        public int id { get; set; }
        public string text { get; set; }
    }

    static class Program
    {
        static void Main()
        {
            using (var db = new LiteDatabase("database.db")) {
                LiteCollection<object> collection = db.GetCollection<object>("testCollection");

                collection.Insert(new Test { text = "aaa" });
                collection.Insert(new Test { text = "bbb" });
            }
        }
    }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
lilasquaredcommented, Nov 14, 2018

Yes your types have an ID property but object does not. LiteDb uses the type given to the getcollection function to know what properties should be used. In your case you have passed object. As I said look into use generics or the dynamic keyword for this

0reactions
lilasquaredcommented, Nov 14, 2018

Happy to help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

LiteDB.LiteException: 'Cannot insert duplicate key in ...
Insert(T document) started throwing the cited exception: LiteDB.LiteException: 'Cannot insert duplicate key in unique index '_id'. The duplicate ...
Read more >
LiteDB - Multiple entries with same ID
I want to be able to insert warnings with the same UserId , like in SQLite, but I get this error: Cannot insert...
Read more >
Service crash randomly
LiteException : Cannot insert duplicate key in unique index '_id'. The duplicate value is '"$/dashboardlog/17\00001"'. at LiteDB.IndexService.
Read more >
Developing a standalone embystat server - Page 25
LiteException: Cannot insert duplicate key in unique index '_id'. The duplicate value is '453207'. at LiteDB.IndexService.AddNode(CollectionIndex index ...
Read more >
Cannot insert duplicate key row in object 'dbo.Items' with ...
System.Exception: Cannot insert duplicate key row in object 'dbo.Items' with unique index 'ndxID'. The duplicate key value is ...
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