title: Programmatically create index
body: <p>How do I create an index programmatically in RavenDB?</p>
  
  <p>I tried to follow this <a href="http://ayende.com/blog/4668/ravendb-defining-indexes" rel="nofollow">example</a>.</p>
  
  <p>This is my index creator:</p>
  
  <pre><code>public class MyIndex : Raven.Client.Indexes.AbstractIndexCreationTask&lt;MyEntity&gt;
  {
      public MyIndex()
      {
          Map = col =&gt; col.Select(c =&gt; new
          {
              code = c.Code,
              len = c.Code.Length,
              sub = c.Code.Substring(0, 1)
          });
      }
  }
  </code></pre>
  
  <p>And here is the caller:</p>
  
  <pre><code>var store = new Raven.Client.Document.DocumentStore
  {
      Url = "http://localhost:8080"
  };
  store.Initialize();
  
  try
  {
      using (var session = store.OpenSession("MyDB"))
      {
          Raven.Client.Indexes.IndexCreation.CreateIndexes(
              typeof(MyIndex).Assembly, store);
      }
  }
  finally
  {
      store.Dispose();
  }
  </code></pre>
  
  <p>The index was created but not in MyDB but in system database.</p>
  
  <p>How to create the index in MyDB? Is the way I create index correct?</p>