Is it OK to open a DB4o file for query, insert, update multiple times?

Posted by Khnle on Stack Overflow See other posts from Stack Overflow or by Khnle
Published on 2010-06-08T15:29:16Z Indexed on 2010/06/08 15:32 UTC
Read the original article Hit count: 267

Filed under:

This is the way I am thinking of using DB4o. When I need to query, I would open the file, read and close:

using (IObjectContainer db = Db4oFactory.OpenFile(Db4oFactory.NewConfiguration(), YapFileName))
{
    try
    {
        List<Pilot> pilots = db.Query<Pilot>().ToList<Pilot>();
    }
    finally
    {
       try { db.Close(); }
       catch (Exception) { };
    }
}

At some later time, when I need to insert, then

using (IObjectContainer db = Db4oFactory.OpenFile(Db4oFactory.NewConfiguration(), YapFileName))
{
    try
    {
        Pilot pilot1 = new Pilot("Michael Schumacher", 100);
        db.Store(pilot1);
    }
    finally
    {
       try { db.Close(); }
       catch (Exception) { };
    }
}

In this way, I thought I will keep the file more tidy by only having it open when needed, and have it closed most of the time. But I keep getting InvalidCastException

Unable to cast object of type 'Db4objects.Db4o.Reflect.Generic.GenericObject' to type 'Pilot'

What's the correct way to use DB4o?

© Stack Overflow or respective owner

Related posts about db4o