Reputation: 1008
As I've read in db4o documentation (.net version), I supposed that to rename a field in a class I would have to add the following to my code (and renaming the actual field in the class, of course):
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.ObjectClass("Namespace.ClassName, AssemblyName").
ObjectField("fieldname").Rename("newfieldname");
And then leave this code there. But I tested this and it didn't work (the objects in the database would not load the renamed field). What did work was to follow the instructions in this page, which are
This seems a bit odd to me. My program needs to run once with just the rename call, and the run without the rename call but with the actual field renamed. The question is: is there a way to do an effective rename in just one step?
The steps that worked may mean a little extra effort, for example, in the process of updating an embedded (mobile) application - before installing the new version (refactored) on client, it would be necessary to run the rename call once. Is there a way to avoid it? Am I missing something or is there other way to rename a fiel in a db4o database?
Upvotes: 2
Views: 278
Reputation: 18670
I also had no luck with renaming in the configuration, but I found the following way that works well:
static void RenameFields() {
using (IObjectContainer db = Db4oEmbedded.OpenFile("mydb.db4o")) {
db.Ext().StoredClass(typeof(MyType)).GetStoredFields().Single(f => f.GetName() == "<ID>k__BackingField").Rename("<Id>k__BackingField");
}
}
MyType
can also be specified using the fully qualified class name as given by the function below, which lists all stored classes and field names. You will notice that private fields generated by auto properties are of the form <PropertyName>k__BackingField
, for example <Id>k__BackingField
for a property named Id
.
static void ShowClassNamesAndFields() {
using (IObjectContainer db = Db4oEmbedded.OpenFile("mydb.db4o")) {
var dbClasses = db.Ext().StoredClasses();
foreach (var dbClass in dbClasses) {
Debug.Print(dbClass.GetName());
foreach (var dbField in dbClass.GetStoredFields()) {
Debug.Print(" " + dbField.GetName());
}
}
}
}
Upvotes: 1