Dave
Dave

Reputation: 1950

Creating new database in code, RavenDB

I'm running RavenDB.Client 2.0.2173-Unstable. I'm creating a multi-tenant system, and as part of my registration process, i would like to create a new Raven database.

I have three simple lines of code..

  string newDBName = "1234";
  IDocumentStore documentStore = new DocumentStore { Url = "http://myserver:8080",    DefaultDatabase = newDbName};
  documentStore.Initialize();
  documentStore.DatabaseCommands.EnsureDatabaseExists(newDBName);

Per suggestion, I also tried this:

  string newDBName = "1234";
  IDocumentStore documentStore = new DocumentStore { Url = "http://myserver:8080"};
  documentStore.Initialize();
  documentStore.DatabaseCommands.EnsureDatabaseExists(newDBName);

I get an InvalidOperationException on the last line, Raven is telling me it could not figure out what to do.

<h1>Could not figure out what to do</h1>

        <p>Your request didn't match anything that Raven knows to do, sorry...</p>

I know my connection/server works, because I'm able to read/write from the default database.

Is it a permissions issue?

Upvotes: 5

Views: 3365

Answers (2)

Rahul P Nath
Rahul P Nath

Reputation: 354

In Ravendb v4, you can use the CreateDatabaseOperation to create a new database on the server.

store.Maintenance.Server.Send(
         new CreateDatabaseOperation(new DatabaseRecord("MyNewDatabase")));

Upvotes: 2

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241450

You can't create a database from the database you're already in. Leave the DefaultDatabase field blank so you connect to the RavenDB system database. You should then be able to create the new tenant database.

You should also make sure to pass the tenant database name when opening sessions, creating indexes, and using DatabaseCommands.

Upvotes: 3

Related Questions