检查 Raven Db 是否存在?

Check if Raven Db exist?

如何以编程方式检查名为 "Test" 的 Raven Db (http://ravendb.net/) 是否存在?

此致

  1. EnsureDatabaseExists 是在 Raven.Client.Extensions 命名空间中定义的 IDatabaseCommands 上的扩展方法。

要使其正常工作,您需要为此命名空间添加一个 using 语句。

 using Raven.Client;
 using Raven.Client.Extensions;

 using (DocumentStore store = new DocumentStore()
 {
     Url = "http://localhost:8080/" ;
 })
 {
     store.Initialize();
    store.DatabaseCommands.EnsureDatabaseExists("SomeDatabase");
}
  1. 这是我为此使用的扩展方法:

    public static bool DatabaseExists(this IDocumentStore documentStore, 
    string databaseName) 
    { 
      var headers = documentStore.DatabaseCommands.Head("Raven/Databases/" + databaseName); 
      return headers != null; 
    } 
    

轻松调用:

bool exists = documentStore.DatabaseExists("foo"); 

这在您的 documentStore 指向 raven 的默认值时有效 系统数据库。如果您在文档存储上设置 DefaultDatabase, 我不相信它会正常工作。