Search code examples
c#db4o

db4o does not delete the record


Good day! Try db4o, faced with this problem: I can not delete records:

using (IObjectServer server = Db4oClientServer.OpenServer(HttpContext.Current.Server.MapPath("~/transfers.data"), 0))
            {
                using (IObjectContainer client = server.OpenClient())
                {
                    var keyValuePair = (from KeyValuePair<DateTime, Transfer> d in client where d.Key < DateTime.Now.AddHours(-3) select d);
                    client.Delete(keyValuePair.First());
                    client.Commit();       
                }
            }

After this code the number of objects (KeyValuePair< DateTime, Transfer >) in the database is not changed.


Solution

  • This will not work! The reason is that a KeyValuePair is a value type, which means it has no identity. However db4o manages objects by their identity! Now C# happily boxes any value type to an object, but that useless for db4o since it won't find any object with the given identity in the database.

    You ran into annoying corner-case between the .NET and db4o behavior. Basically there is no nice work around for this, especially since db4o doesn't have an API to delete a object by its internal id =(.

    For the future. Don't store KeyValuePairs (or any struct) for itself. Only as part of another object. (and use 8.1, it has a bugfix preventing it from never deleting structs). That avoids this issue.