The name 'NodaTimeField' does not exist in the current context 在 RavenDB 上安装索引时出错

The name 'NodaTimeField' does not exist in the current context error during installation of index on RavenDB

我在 RavenDB 索引中使用 NodaTime 的 LocalDate。 这是索引的示例:

public class TaskIndex : AbstractIndexCreationTask<ScheduleTask>
{
    public TaskIndex()
    {
        Map = tasks => from task in tasks
            select new
            {
                task.Name,
                PlannedStartDate = task.PlannedStartDate.AsLocalDate().Resolve(),
                PlannedDueDate = task.PlannedDueDate.AsLocalDate().Resolve()
            };

        Index(x => x.Name, FieldIndexing.Analyzed);
        Store(x => x.Name, FieldStorage.Yes);
        TermVector(x => x.Name, FieldTermVector.WithPositionsAndOffsets);
    }
}

我按照 here 所述安装了 RavenDB-NodaTime 包。

这是我用来安装索引的一段代码:

var assembly = AppDomain.CurrentDomain.Load(new AssemblyName
{
    Name = "cs.Scheduling"
});
var catalog = new AssemblyCatalog(assembly);
var provider = new CompositionContainer(catalog);
var commands = documentStore.DatabaseCommands.ForDatabase(dbName);
IndexCreation.CreateIndexes(provider, commands, documentStore.Conventions);

documentStore 配置了默认数据库,但随后我用它来将索引安装到不同的(租户)数据库名称中,dbName.

安装索引时出现异常:The name 'NodaTimeField' does not exist in the current context.

我有一个默认数据库,它与我尝试为其安装索引的数据库完全不同。所以基本上情况与描述的情况相似 here 但我使用的是独立版本的 RavenDB 服务器。

我试图找出我可以在那里做的建议,但没能做到:

embeddableDocumentStore.ServerIfEmbedded.Options.DatabaseLandlord.SetupTenantConfiguration += configuration =>
            {
                configuration.Catalog.Catalogs.Add(new TypeCatalog(typeof(DeleteOnConflict)));
                configuration.Catalog.Catalogs.Add(new TypeCatalog(typeof(PutOnConflict)));
            };

我使用的 RavenDB 版本是 2.5.2956。
RavenDB.Client.NodaTime - 2.5.10。

希望得到您的帮助。谢谢。

我相信答案是您的租户数据库没有包 "activated" 您的数据库文档(在 Raven 3 的设置下)应该有类似

的东西
"Raven/ActiveBundles": "Encryption;Compression;NodaTime"

你还必须打电话给

store.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

我在 store.Initialize() 之后调用它。完成这两项操作后,您可能必须通过重新保存文档来修复现有数据(不确定是否有其他方法)。新数据将以“2016-2-3”格式正确存储,这将使您的索引成为 return 数据。

就我而言,这是一个非常愚蠢的错误。当我前段时间安装 RavenDB 服务器时,我将它安装到 non-default 目的地。后来一些 RavenDB 更新被安装到默认目的地(即 \Program Files (x86)\RavenDB)。当我安装 RavenDB-NodaTime 捆绑包时,我将其放入了错误的目的地 (\Program Files (x86)\RavenDB)。

在检测到这个问题并在我的正确目的地正确配置 RavenDB 服务器后,标题中描述的错误已经消失。

希望这个回答可以帮助到其他人。

P.S. 后来从数据库读取数据时出现反序列化错误(RavenDB 不知道如何从 "yyyy-MM-dd" 中的字符串反序列化日期格式为 LocalDate object) 我通过在 store.Initialize(); 调用 Steven suggested in his answer.

之后调用 store.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); 来修复