You are here: Basics Operations & Concepts > Update Concept > Updating Collections

Updating Collections

From the db4o perspective collections behave like ordinary objects. This means that the update-depth also applies to collections. When you change a collection and store the object which contains it, the changes are not stored by default.

using (IObjectContainer container = Db4oEmbedded.OpenFile(DatabaseFile))
{
    Person jodie = QueryForJodie(container);
    jodie.Add(new Person("Jamie"));
    // Remember that a collection is also a regular object
    // so with the default-update depth of one, only the changes
    // on the person-object are stored, but not the changes on
    // the friend-list.
    container.Store(jodie);
}
using (IObjectContainer container = Db4oEmbedded.OpenFile(DatabaseFile))
{
    Person jodie = QueryForJodie(container);
    foreach (Person person in jodie.Friends)
    {
        // the added friend is gone, because the update-depth is to low
        Console.WriteLine("Friend=" + person.Name);
    }
}
UpdateDepthPitfall.cs: Update doesn't work on collection
Using container As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFile)
    Dim jodie As Person = QueryForJodie(container)
    jodie.Add(New Person("Jamie"))
    ' Remember that a collection is also a regular object
    ' so with the default-update depth of one, only the changes
    ' on the person-object are stored, but not the changes on
    ' the friend-list.
    container.Store(jodie)
End Using
Using container As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFile)
    Dim jodie As Person = QueryForJodie(container)
    For Each person As Person In jodie.Friends
        ' the added friend is gone, because the update-depth is to low
        Console.WriteLine("Friend=" & person.Name)
    Next
End Using
UpdateDepthPitfall.vb: Update doesn't work on collection

For collections the same rules and settings work as for regular objects. For example when you increase the update depth to two, you can store the parent object and the changes of the collection are persisted as well.

IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
config.Common.UpdateDepth = 2;
using (IObjectContainer container = Db4oEmbedded.OpenFile(DatabaseFile))
{
UpdateDepthPitfall.cs: A higher update depth fixes the issue
Dim config As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
config.Common.UpdateDepth = 2
Using container As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFile)
UpdateDepthPitfall.vb: A higher update depth fixes the issue