tags:

views:

137

answers:

1

I just started using db4o on C#, and I'm having trouble setting the UniqueConstraint on the DB..

here's the db4o configuration

static IObjectContainer db = Db4oEmbedded.OpenFile(dbase.Configuration(), "data.db4o");
static IEmbeddedConfiguration Configuration()
{
    IEmbeddedConfiguration dbConfig = Db4oEmbedded.NewConfiguration();
    // Initialize Replication
    dbConfig.File.GenerateUUIDs = ConfigScope.Globally;
    dbConfig.File.GenerateVersionNumbers = ConfigScope.Globally;
    // Initialize Indexes
    dbConfig.Common.ObjectClass(typeof(DAObs.Environment)).ObjectField("Key").Indexed(true);
    dbConfig.Common.Add(new Db4objects.Db4o.Constraints.UniqueFieldValueConstraint(typeof(DAObs.Environment), "Key"));
    return dbConfig;
}

and the object to serialize:

class Environment
{
    public string Key { get; set; }
    public string Value { get; set; }
}

everytime I get to commiting some values, an "Object reference not set to an instance of an object." Exception pops up, with a stack trace pointing to the UniqueFieldValueConstraint. Also, when I comment out the two lines after the "Initialize Indexes" comment, everything runs fine (Except you can save non-unique keys, which is a problem)~

Commit code (In case I'm doing something wrong in this part too:)

public static void Create(string key, string value)
{
    try
    {
        db.Store(new DAObs.Environment() { Key = key, Value = value });
        db.Commit();
    }
    catch (Db4objects.Db4o.Events.EventException ex)
    {
        System.Console.WriteLine
            (DateTime.Now +  " :: Environment.Create\n" + ex.InnerException.Message +"\n" + ex.InnerException.StackTrace);
        db.Rollback();
    }
}

Help please? Thanks in advance~

+2  A: 

I forgot that C# is using weird backing fields for the property shortcuts :( Updated the configuration to the following:

// Initialize Indexes
dbConfig.Common.ObjectClass(typeof(DAObs.Environment))
    .ObjectField("<Key>k__BackingField").Indexed(true);
dbConfig.Common.Add(new Db4objects.Db4o.Constraints.
    UniqueFieldValueConstraint(typeof(DAObs.Environment), "<Key>k__BackingField"));

Everything works fine now~ ^^

GaiusSensei
+2 Thanks! I use Java. Is Java affected by the backing fields?
Viet
Does Java have properties, at all? Isn't Java using all those getter and setter methods, or am I lagging a few years behind?
mnemosyn
Thanks for this topic and answer with the "trick"
binball