如何以编程方式检查我的Raven Db(http://ravendb.net/)是否已调用"测试"存在?
祝你好运
答案 0 :(得分:5)
EnsureDatabaseExists
是IDatabaseCommands
命名空间中定义的Raven.Client.Extensions
上的扩展方法。 要使其工作,您需要为此命名空间添加using语句。
using Raven.Client;
using Raven.Client.Extensions;
using (DocumentStore store = new DocumentStore()
{
Url = "http://localhost:8080/" ;
})
{
store.Initialize();
store.DatabaseCommands.EnsureDatabaseExists("SomeDatabase");
}
这是我用于此的扩展方法:
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指向乌鸦的默认值时,这是有效的 系统数据库。如果在文档存储上设置DefaultDatabase, 我不相信它会正常工作。