【问题标题】:RavenDB DateTime error after updating to 2.5.5.2700更新到 2.5.5.2700 后 RavenDB DateTime 错误
【发布时间】:2013-09-08 17:24:44
【问题描述】:

更新到最新的 RavenDB 后出现此错误:

[InvalidCastException:无法将“Index_Auto_2fDocuments_2fByCreationDateTimeAndPublishTimeSortByCreationDateTimePublishTime”类型的对象转换为“Raven.Database.Linq.AbstractViewGenerator”类型。] c:\Builds\RavenDB-Stable\Raven.Database\Linq\DynamicViewCompiler.cs:598 中的 Raven.Database.Linq.DynamicViewCompiler.GenerateInstance() c:\Builds\RavenDB-Stable\Raven.Database\Storage\IndexDefinitionStorage.cs:233 中的 Raven.Database.Storage.IndexDefinitionStorage.AddAndCompileIndex(IndexDefinition indexDefinition) c:\Builds\RavenDB-Stable\Raven.Database\DocumentDatabase.cs:1207 中的 Raven.Database.DocumentDatabase.PutIndex(字符串名称,IndexDefinition 定义) Raven.Database.Queries.DynamicQueryRunner.CreateAutoIndex(String PermanentIndexName, Func1 createDefinition) in c:\Builds\RavenDB-Stable\Raven.Database\Queries\DynamicQueryRunner.cs:170 Raven.Database.Queries.DynamicQueryRunner.GetAppropriateIndexToQuery(String entityName, IndexQuery query, DynamicQueryMapping map) in c:\Builds\RavenDB-Stable\Raven.Database\Queries\DynamicQueryRunner.cs:158 Raven.Database.Queries.DynamicQueryRunner.ExecuteDynamicQuery(String entityName, IndexQuery query) in c:\Builds\RavenDB-Stable\Raven.Database\Queries\DynamicQueryRunner.cs:32 Raven.Client.Embedded.EmbeddedDatabaseCommands.Query(String index, IndexQuery query, String[] includes, Boolean metadataOnly, Boolean indexEntriesOnly) in c:\Builds\RavenDB-Stable\Raven.Client.Embedded\EmbeddedDatabaseCommands.cs:481 Raven.Client.Document.AbstractDocumentQuery2.ExecuteActualQuery() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\AbstractDocumentQuery.cs:664 Raven.Client.Document.AbstractDocumentQuery2.get_QueryResult() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\AbstractDocumentQuery.cs:633 Raven.Client.Linq.RavenQueryProviderProcessor1.ExecuteQuery() 在 c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:1499 Raven.Client.Linq.RavenQueryInspector1.GetEnumerator() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryInspector.cs:105 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +446 System.Linq.Enumerable.ToList(IEnumerable1 源) +80 C:\Dev\Repos\Git\Writer\Kernel\DocumentRavenProvider.cs:85 中的 Kernel.DocumentRavenProvider.GetDocuments(Int32 计数) C:\Dev\Repos\Git\Writer\Kernel\DocumentManager.cs:46 中的 Kernel.DocumentManager.GetDocuments(Int32 计数) c:\Dev\Repos\Git\Writer\Blog\Controllers\DocumentController.cs:18 中的 Blog.Controllers.DocumentController.Index() lambda_method(闭包,ControllerBase,对象[])+79 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +261 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 参数) +39 System.Web.Mvc.Async.c_DisplayClass42.b_41() +34 System.Web.Mvc.Async.c_DisplayClass39.b_33() +124 System.Web.Mvc.Async.c_DisplayClass4f.b_49() +838923 System.Web.Mvc.Async.c_DisplayClass37.b_36(IAsyncResult asyncResult) +15 System.Web.Mvc.Async.c_DisplayClass2a.b_20() +33 System.Web.Mvc.Async.c_DisplayClass25.b_22(IAsyncResult asyncResult) +839508 System.Web.Mvc.c_DisplayClass1d.b_18(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +15 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65 System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +15 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51 System.Web.Mvc.c_DisplayClass8.b_3(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +15 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

执行此查询时

documents = session.Query<Model.Document>().Where(d => d.PublishTime <= DateTime.Now)
.OrderByDescending(document=>document.CreationDateTime).Take(count)

我正在使用嵌入式数据库,我已经尝试过

  1. 删除数据库并重新启动 asp.net 应用程序。 同样的错误
  2. 向数据库中添加一些内容,然后重新启动并加载 asp.net 应用程序。 同样的错误

更新前,一切正常。我有很多单元测试,我针对内存中的 Raven 数据库进行测试。这里保存和检索工作正常,所以可能与正在解析的json有关?

我有一个调用底层 RavenDB 保存逻辑的控制器:

// POST api/document
        public async System.Threading.Tasks.Task<Model.Document> Post([FromBody]Model.Document document)
        {
            var savedDocument = await _documentManager.Save(document);

            var hubConnection = new Microsoft.AspNet.SignalR.Client.Hubs.HubConnection(BaseSiteUrl);
            var hub = hubConnection.CreateHubProxy("DocumentHub");
            hubConnection.Start().Wait();
            hub.Invoke("NewArticle", savedDocument.Id).Wait();

            return document;
        }

我的模型看起来像这样

    public class Document
    {
        public int Id { get; set; }
        public string Headline { get; set; }
        public string Text { get; set; }
        public DateTime CreationDateTime { get; set; }
        public DateTime PublishTime { get; set; }
        public HashSet<string> Tags { get; private set; }
        public int CategoryId { get; set; }

        [Raven.Imports.Newtonsoft.Json.JsonIgnore]
        public Model.Category Category { get; private set; }

        public Document()
        {
            CreationDateTime = DateTime.Now;
            Tags = new HashSet<string>();
        }

        public Document(string headline, string text, DateTime creationTime, DateTime publishTime)
        {
            Headline = headline;
            Text = text;
            CreationDateTime = creationTime;
            PublishTime = publishTime;
            Tags = new HashSet<string>();
        }

        public string GetCategoryName()
        {
            string name = String.Empty;

            if (this.Category != null)
            {
                name = this.Category.Name;
            }

            return name;
        }

        public void SetCategory(Model.Category category)
        {
            if (category != null)
            {
                this.Category = category;
                this.CategoryId = category.Id;
            }
        }

        public string GetTeaser()
        {
            string teaser = Text;
            if (Text.Count() > 250)
                teaser = Text.Substring(0, 250) + " ...";

            return teaser;
        }

        public string GetCommaSeperatedTagsString()
        {
            var tags = new StringBuilder();

            int i = 0;
            foreach (string tag in Tags)
            {
                if (!String.IsNullOrEmpty(tag))
                {
                    tags.Append(tag);

                    if (i < Tags.Count - 1)
                    {
                        tags.Append(",");
                    }
                }
                i++;

            }

            return tags.ToString();
        }

        public string GetSpaceSeperatedTagsString()
        {
            var tags = new StringBuilder();

            int i = 0;
            foreach (string tag in Tags)
            {
                if (!String.IsNullOrEmpty(tag))
                {
                    tags.Append(tag);

                    if (i < Tags.Count - 1)
                    {
                        tags.Append(" ");
                    }
                }
                i++;

            }

            return tags.ToString();
        }

        public void AddTagsString(string tagsString)
        {
            string[] tagsSplitted = tagsString.Split(',');

            foreach (var tag in tagsSplitted)
            {
                if (!String.IsNullOrEmpty(tag))
                {
                    Tags.Add(tag);
                }
            }
        }

        public string GetTimeString()
        {
            System.TimeSpan span = DateTime.Now.Subtract(this.CreationDateTime);
            string time = "lige udgivet";

            if (span.Days > 0)
            {
                if (span.Days > 1)
                    time = span.Days + " dage siden";
                else
                    time = "en dag siden";
            }

            else if (span.Hours > 0)
            {
                if (span.Hours > 1)
                    time = span.Hours + " timer siden";
                else
                    time = "en time siden";
            }

            else if (span.Minutes > 0)
            {
                if (span.Minutes > 1)
                    time = span.Minutes + " minutter siden.";
                else
                    time = "et minut siden.";
            }

            else if (span.Seconds > 0)
            {
                time = "lige udgivet";
            }
            else if (span.Milliseconds > 0)
            {
                time = "lige udgivet";
            }

            return time;
        }
    }

public class Category
            {
                public string Name { get; set; }
                public int Id { get; set; }
            }

但是,如果我在上面的控制器方法中中断,json 转换就好了。这里没有错误。

RavenDB 是否改变了处理 DateTime 的方式?有谁知道解决方案,或者一篇文章可以为我指明正确的方向。

更新 我可以看到这只发生在我尝试查询数据库并获取文档时。这是在一个空数据库和一个带有文档的数据库上测试的。我可以看到保存方法工作得很好。

【问题讨论】:

  • 更新什么版本?另请参阅ravendb.net/kb/61/working-with-date-and-time-in-ravendb
  • 我通过 Nuget 从版本 2.5.2666 更新。在那个版本中一切正常。我没有更新我的代码库,只是更新了 RavenDB,重新编译了代码库并重新启动了我的 IIS。
  • 在 2666 和 2700 之间的日期时间肯定没有重大变化。但也许索引中有一些东西。尝试删除自动索引并让它自行重建。我假设当您从一个干净的空数据库开始时不会发生问题,对吧?
  • 当我使用一个干净的空数据库时发生错误。
  • 我试图重现它,但我没有看到您遇到的错误。请查看我的单元测试here,如果您正在做一些完全不同的事情,请告诉我。

标签: c# asp.net .net ravendb


【解决方案1】:

已通过从 nuget 卸载 Raven.Embedded 并重新安装来解决此问题。

【讨论】:

  • 我在内存数据库中遇到了完全相同的错误,但卸载对我不起作用。我确实注意到,通过在调用 .Where 之前使用 .List() 评估查询似乎有效。我尚未确认 Web 实例中的情况。
猜你喜欢
  • 2013-01-31
  • 2013-07-19
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2021-02-13
  • 2019-04-03
相关资源
最近更新 更多