2013-10-28 119 views
1
[Test] 
public void Can_Get_All() 
{ 
    var repository = new RavenRepository<Motorcycle>(); 
    repository.DeleteAll(); 

    repository.Store(new Motorcycle {Make = "Datsun", YearManufactured = 1972}); 
    repository.Store(new Motorcycle {Make = "Toyota", YearManufactured = 2002}); 

    IList<Motorcycle> savedThings = repository.GetAll(); 

    Assert.IsTrue(savedThings.Count == 2); 
} 

RavenRepository.GetAll()如何从RavenDB获取所有文档?

public IList<T> GetAll() 
{ 
    using (IDocumentSession session = _collection.OpenSession()) 
    { 
     return session.Query<T>().ToList(); // Throws exception 
    } 
} 

运行此测试抛出异常:

Raven.Abstractions.Exceptions.IndexCompilationException:无法理解的查询:变量初始值选择必须具有lambda表达式用物体创造表情

为什么?我如何才能从RavenDB中获取T类型的所有文档?

+0

我会阅读文档,但http://ravendb.net/已关闭了两个小时... –

回答

2

如果你想要的是删除一切,那么你可以这样做:

public class AllDocumentsById : AbstractIndexCreationTask 
{ 
    public override IndexDefinition CreateIndexDefinition() 
    { 
     return 
      new IndexDefinition 
      { 
       Name = "AllDocumentsById", 
       Map = "from doc in docs 
         let DocId = doc[\"@metadata\"][\"@id\"] 
         select new {DocId};" 
      }; 
    } 
} 

docStore.DatabaseCommands.DeleteByIndex("AllDocumentsById", new IndexQuery()); 

如果你有,你要根据删除不同的索引那么将正常运行好。我们也正在使用这种模式进行一些测试。

1

由于RavenDB强制执行默认分页,所以不起作用。看看这里:http://ayende.com/blog/161249/ravendbs-querying-streaming-unbounded-results

+0

好吧,如果我只想要其中的100个呢?这里的问题不是我的结果集是有界的,而是这个代码抛出一个异常。 session.Query ().Take(200).ToList()引发相同的异常。 –

+0

存储库模式的使用使得很难理解引擎盖下正在发生什么。你在查询之前是否调用SaveChanges? – synhershko

相关问题