2012-08-07 53 views
1

我想写多租户ravendb通用删除功能进行集成测试的类 -删除收录在RavenDB没有找到

public class RavenDeleteAll 
{ 
    private readonly IDocumentStore _store; 
    private readonly string _testDataBase; 

    public RavenDeleteAll(string testDataBase, IDocumentStore store) 
    { 
     _testDataBase = testDataBase; 
     _store = store; 
    } 

    public void Clear<T>(string indexName) 
    { 
     using (var session = _store.OpenSession(_testDataBase)) 
     { 
      session.Advanced.DocumentStore.DatabaseCommands.DeleteIndex(indexName); 

      session.Advanced.DocumentStore.DatabaseCommands.PutIndex(indexName, new IndexDefinitionBuilder<T> 
                         { 
                          Map = documents => documents.Select(entity => new { }) 
                         }); 
      var indexDefinition = session.Advanced.DocumentStore.DatabaseCommands.GetIndex(indexName); 
      session.Advanced.LuceneQuery<T>(indexName) 
      .WaitForNonStaleResultsAsOfNow() 
      .Take(0) 
      .ToList(); 

      session.Advanced.DatabaseCommands.DeleteByIndex(indexName, new IndexQuery()); 


     } 

    } 
} 

注意,在代码中,我尝试读回putindex后的指数要求进行完整性检查。但是当我执行索引时,它会抛出一个无效操作异常,指出/ indexes/UTO不存在?

而且从管理控制台,我可以清楚地看到指数 - enter image description here

什么我不这样做?此外,索引是在默认数据库下创建的,而不是实际的数据库名称?

回答

2

它看起来像是在默认数据库而不是租户数据库中创建索引,然后向Tenant数据库询问该索引。您需要在要使用它的数据库中创建索引。以下内容未经测试,但适用于在租户数据库中创建索引。

IDatabaseCommands context = session.Advanced.DocumentStore.DatabaseCommands.ForDatabase(database); 
context.PutIndex(indexName, new IndexDefinitionBuilder<T> 
    { 
     Map = documents => documents.Select(entity => new { }) 
    }); 
+0

这工作就像魔术一样。谢谢sooooo深夜救了我。我想吻你,但那不合适。五颗星给这个答案!!!!! – NiladriBose 2012-08-09 09:47:15