Oid mapping example crashes when storing multiple objects


I'm using NDatabase 3.8 and I wanted to use ids generated by the database so I tried the example from the documentation (chapter 4.15).
I tried both with long and with OID object and in both cases it crashes when I try to store the object for the second time (in case of long, it complains that -1 is a duplicate key).

This is the class which I'm saving:
  class A
    private readonly long _oid;

    public long Oid { get { return _oid; } }
    public string Text;
    public A(string input)
      _oid = -1;
      Text = input;

    public override string ToString()
      return Text;

And this is the code for storing and retrieving:
  class Program
    static void Main(string[] args)
      var dbName = "test.ndb";

      using (var odb = OdbFactory.Open(dbName))
        odb.IndexManagerFor<A>().AddUniqueIndexOn("OidIndex", "_oid");

      var ids = new List<long>();
      using (var odb = OdbFactory.OpenLast())
        ids.Add(odb.Store(new A("a1")).ObjectId);

      using (var odb = OdbFactory.OpenLast())
        ids.Add(odb.Store(new A("a2")).ObjectId);

      using (var odb = OdbFactory.OpenLast())
        Console.WriteLine(odb.QueryAndExecute<A>().FirstOrDefault(entity => entity.Oid == ids[1]));

To reproduce the issue just run the program and see where it crashes.

So could you please look into this.
Thank you and best regards


Renesis wrote Nov 20, 2014 at 6:13 PM

Also, one more thing that I forgot to highlight (which wasn't included in the documentation) is that we also want to create the index on this field so that retrievals are faster.
If this is not the right way, could you please provide the fastest alternative for retrieving and storing entities while using auto generated ids.