开发者

db4o, how to update an object if a field was added?

开发者 https://www.devze.com 2023-01-17 14:43 出处:网络
How can I do this? I stored an obj开发者_如何学Goect in db4o, e.g: class Person { string _name; int _age;

How can I do this?

I stored an obj开发者_如何学Goect in db4o, e.g:

class Person {
    string _name;
    int _age;
}

now, after hundrets of Persons stored in the db, I've added a new field:

class Person {
    string _name;
    int _age;
    bool? _newField;
}

When I load the old classes with the new class, the _newField will be null or the default value. When I save it back to the db, the added field is obmitted.

How can I update all existing objects with the new field? Is that possible?


As you said, when you add a new field it has the default value for existing object. Now you just can load a object, set a value to the field and store it again. Then the data is stored for that field. See also on the documentation.

So you should be able to load a object and update it:

IObjectContainer container = ...
var persons = from Person p in container                  
              select p;

foreach(var p in persons){
     p.NewField = true; // new value
     container.Store(p);
}

// done

I think the default value for a Nullable bool should be 'empty'.

If that doesn't work, it looks like a bug. Which version are you using?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号