是否可以通过IndexCreation.CreateIndexes创建包含泛型类型的RavenDB Map-Reduce索引?

问题描述 投票:0回答:1

我有一个索引:


public class TotalsIndex<TClass> : AbstractIndexCreationTask<TClass, Totals> where TClass : class, IClass

我收到错误:

Cannot create an instance of Raven.Client.Documents.Indexes.AbstractIndexCreationTask
1[TDocument] 因为 Type.ContainsGenericParameters 为 true。'`

使用时

IndexCreation.CreateIndexes(assembly, store);

是否可以通过 IndexCreation.CreateIndexes 创建包含泛型类型的 RavenDB Map-Reduce 索引?

另一方面

new TotalsIndex<TClass>().Execute(documentStore);
有效

ravendb
1个回答
0
投票

定义 MapMap-Reduce 索引的常见方法是不使用索引类名称的通用名称。

例如,请参阅此 Map-Reduce 索引示例:
(摘自https://demo.ravendb.net/demos/csharp/static-indexes/map-reduce-index

public class Employees_ByCountry : 
    AbstractIndexCreationTask<Employee, Employees_ByCountry.IndexEntry>
{
    public class IndexEntry
    {
        public string Country { get; set; }
        public int CountryCount { get; set; }
    }
            
    public Employees_ByCountry()
    {
        Map = employees => from employee in employees
            select new IndexEntry
            {
               Country = employee.Address.Country,
               CountryCount = 1
            };
                
        Reduce = results => from result in results
            group result by result.Country into g
            select new IndexEntry
            {
                Country = g.Key,
                CountryCount = g.Sum(x => x.CountryCount)
            };
    }
}

然后您可以通过以下方式部署索引

new Employees_ByCountry().Execute(store);

或通过:

IndexCreation.CreateIndexes(new[] { new Employees_ByCountry() }, store);

或者 - 如你所愿 - 通过:

IndexCreation.CreateIndexes(assembly, store);

或通过:
在商店发送

PutIndexesOperation


请参阅创建和部署
https://ravendb.net/docs/article-page/6.0/csharp/indexes/creating-and-deploying

© www.soinside.com 2019 - 2024. All rights reserved.