RavenB选择数据库

时间:2012-11-07 21:30:53

标签: ravendb

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应用程序。)但我希望它将数据保存到另一个我创建了乌鸦管理工作室(网页)的数据库中。我在哪里指定数据库名称?另请告诉我如何在配置文件中保存连接字符串和数据库名称。这就是我如何将它保存到没有数据库名称的配置文件

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

3 个答案:

答案 0 :(得分:3)

documentation

中解释了您的所有问题
new DocumentStore 
{
    ConnectionStringName = "Local"
}
<connectionStrings>
    <add name="Local" connectionString="DataDir=~\Data;Database=MyDatabaseName"/>
    <add name="Server" connectionString="Url=http://localhost:8080;Database=MyDatabaseName"/>
 </connectionStrings>

答案 1 :(得分:2)

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

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

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

using (var session = store.OpenSession("the database name")
{
    ...
}

答案 2 :(得分: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();
}