我在 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。
希望得到您的帮助。谢谢。
最佳答案
就我而言,这是一个非常愚蠢的错误。当我前段时间安装 RavenDB 服务器时,我将它安装到了非默认目标位置。后来一些 RavenDB 更新被安装到默认目的地(即 \Program Files (x86)\RavenDB
)。当我安装 RavenDB-NodaTime 包时,我把它放到了错误的目的地( \Program Files (x86)\RavenDB
)。
在检测到此问题并在我的正确目的地正确配置 RavenDB 服务器后,标题中描述的错误已消失。
希望这个答案可以帮助别人。
附言后来从 db 读取数据时出现反序列化错误(RavenDB 不知道如何将日期从 "yyyy-MM-dd"
格式的字符串反序列化为 LocalDate
对象),我通过调用 store.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
修复了该错误。在 store.Initialize();
之后调用Steven在他的 answer 中建议.
关于ravendb - 在 RavenDB 上安装索引期间,当前上下文中不存在名称 'NodaTimeField' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35011726/