2
var store = new DocumentStore()
{
    Url = @"http://localhost"
};
store.Initialize();


Blog blog = new Blog()
{
    Title = "Hello RavenDB",
    Category = "RavenDB",
    Content = "This is a blog about RavenDB",
    Comments = new BlogComment[]{
        new BlogComment() { Title = "Unrealistic", Content= "This example is unrealistic"},
        new BlogComment() { Title = "Nice", Content= "This example is nice"}
    }
};

using (IDocumentSession session = store.OpenSession())
{
    session.Store(blog);
    session.SaveChanges();
}

上面的代码将数据保存到默认数据库。(它是一个 Web 应用程序。)但我希望它将数据保存到我创建的 raven 管理工作室(网页)的另一个数据库中。我在哪里指定数据库名称?另外请告诉我如何将连接字符串与数据库名称一起保存在配置文件中。这就是我将它保存到没有数据库名称的配置文件的方式

<connectionStrings>
  <add name="Local" connectionString="DataDir = ~\Data"/>
  <add name="Server" connectionString="Url = http://localhost:8080"/>
</connectionStrings>
4

3 回答 3

3

您的所有问题都在文档中进行了解释:

new DocumentStore 
{
    ConnectionStringName = "Local"
}
<connectionStrings>
    <add name="Local" connectionString="DataDir=~\Data;Database=MyDatabaseName"/>
    <add name="Server" connectionString="Url=http://localhost:8080;Database=MyDatabaseName"/>
 </connectionStrings>
于 2012-11-07T21:58:11.837 回答
2

其他答案都可以,但为了提高效率,您真的只需要一个 DocumentStore 实例用于您的应用程序,除非您运行多个 Raven 服务器,然后每台服务器都有一个是可以接受的。

如果你只是连接到同一台服务器上的不同数据库,你应该使用:

var store = new DocumentStore(...your connection string or inline options...);

using (var session = store.OpenSession("the database name")
{
    ...
}
于 2012-11-08T00:21:33.353 回答
1

您可以保留显示的连接字符串数据,最好使用最后的数据库名称:

<connectionStrings>
  <add name="Core" connectionString="Url=http://localhost:8082/databases/Core"
    providerName="My primary database." />
  <add name="Backup" connectionString="Url=http://localhost:8082/databases/Backup"
    providerName="My backup stuff." />
</connectionStrings>

接下来,您可以实现单例类,它将保留所有已定义源的处理程序,例如:

public class DocumentStoreProvider : IDocumentStoreProvider
{
    private static readonly IDictionary<string, IDocumentStore> _documentStores = new Dictionary<string, IDocumentStore>();    
    private static readonly DocumentStoreProvider _instance = new DocumentStoreProvider();

    private DocumentStoreProvider()
    {
        var connectionStrings = ConfigurationManager.ConnectionStrings;
        foreach (ConnectionStringSettings connectionString in connectionStrings)
        {
            var name = connectionString.Name;
            var connection = connectionString.ConnectionString;

            IDocumentStore currentStore = new DocumentStore { ConnectionStringName = name };
            currentStore.Initialize();
            currentStore.DatabaseCommands.EnsureDatabaseExists(name);
            IndexCreation.CreateIndexes(Assembly.Load("Your.Assembly.Containing.Indexes"), currentStore);

            _documentStores.Add(name, currentStore);
        }
    }

    public static DocumentStoreProvider Instance
    {
        get { return _instance; }
    }

    public IDocumentStore GetDocumentStore(string key)
    {
        return _documentStores[key];
    }
}

用法可以如下:

using (var session = DocumentStoreProvider.Instance.GetDocumentStore("Backup").OpenSession())
{
    /* do stuff for chosen database... */
    session.Store(something);
    session.SaveChanges();
}
于 2012-11-07T22:04:59.380 回答