1

我们在项目中使用 Db4o。

我有一些自动测试,测试对象的持久性。问题是,我无法两次打开/创建数据库。我有两种获取对象容器的辅助方法。但是当第二次调用该方法时,“ArgumentException:配置已使用”。被抛出。我当然关闭并处理了以前的对象容器。

我做错了什么?

代码:

public static IObjectContainer GetEmptyTestingDatabase() {
    var tempDir = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
    string dbFilePath = Path.Combine(tempDir, "UNIT-TESTING.db4o");
    if (File.Exists(dbFilePath)) {
        File.Delete(dbFilePath);
    }

    var cfg = Db4oFactory.Configure();
    cfg.Add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy()));
    cfg.Add(new TransparentActivationSupport());

    var db = Db4oFactory.OpenFile(cfg, dbFilePath);
    return db;
}
public static IObjectContainer GetMemoryDatabase() {
    string dbFileName = Guid.NewGuid().ToString().ToString();

    var cfg = Db4oFactory.Configure();
    cfg.Storage = new Db4objects.Db4o.IO.PagingMemoryStorage();
    cfg.Add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy()));
    cfg.Add(new TransparentActivationSupport());

    var db = Db4oFactory.OpenFile(cfg, dbFileName);
    return db;
}
4

1 回答 1

1

您正在使用已弃用的 db4o 方法。问题是 Db4oFactory.Configure() 返回一个静态配置对象;这种方法只是为了向后兼容。

如果您使用较新的 db4o 版本,请改用 Db4oEmbedded.NewConfiguration()。否则(如果您真的需要坚持使用较旧的 db4o 版本)使用 Db4oFactory.NewConfiguration() 也应该可以工作。

于 2012-10-24T20:21:59.320 回答