UniqueConstraint in EmbeddedConfiguration
Posted
by LantisGaius
on Stack Overflow
See other posts from Stack Overflow
or by LantisGaius
Published on 2010-03-28T23:53:51Z
Indexed on
2010/03/29
1:23 UTC
Read the original article
Hit count: 548
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~
© Stack Overflow or respective owner