简体   繁体   English

如何从RavenDB数据库中获取所有实体名称(集合名称)?

[英]How can I get all entity names (collection names) from a RavenDB database?

I need retrieve all RavenDB collection names present into a database. 我需要检索存在于数据库中的所有RavenDB集合名称。 Is there any way to do this? 有没有办法做到这一点? How? 怎么样?

If you've used the studio at all, you'll see it created an index called Raven/DocumentsByEntityName . 如果你完全使用了工作室,你会看到它创建了一个名为Raven/DocumentsByEntityName的索引。 If you don't use the studio, or you want to ensure it's there, you can build it yourself with: 如果您不使用工作室,或者您想确保它在那里,您可以自己构建它:

documentStore.ExecuteIndex(new RavenDocumentsByEntityName());

The way that Raven Studio builds it's "collections" list is by getting the terms of the Tag field from this index. Raven Studio构建它的“集合”列表的方式是从此索引中获取Tag字段的条款。 You can do the same thing: 你可以做同样的事情:

var results = documentStore.DatabaseCommands
                           .GetTerms("Raven/DocumentsByEntityName", "Tag", "", 1024);

(If you expect more than 1024 different entity types for some reason, then paginate.) (如果由于某种原因预计会有超过1024种不同的实体类型,那么就会分页。)

Finally I got a solution by trying with my knowledges acquired learning documentation previously. 最后,我通过尝试使用我的知识获取学习文档来获得解决方案。 I would want to advert people I only come here when I'm exausted with tentatives. 我想广告那些我只是在试探性的时候来到这里的人。

public string[] getCollectionNames()
        {
            using (var session = this.store.OpenSession())
            {
                return session.Advanced.LuceneQuery<dynamic>()
                           .SelectFields<dynamic>("@metadata")
                           .Select<dynamic, string>(x => x["@metadata"]["Raven-Entity-Name"])
                           .Distinct()
                           .ToArray();

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM