November Happy Hour will be moved to Thursday December 5th.

Code and database both say a property is XHtml, but still complains about being unable to convert XhtmlString to String

Vote:
 

Hello,

We are experiening a strange problem with some of or content pages when retrieving the content:

[InvalidCastException: Unable to cast object of type 'EPiServer.Core.XhtmlString' to type 'System.String'.]
   EPiServer.DataAbstraction.RuntimeModel.Internal.ContentModelMethods.GetValueOrDefault(PropertyDataCollection propertyDataCollection, String propertyName, Boolean defaultOnIsNull) +111
   Project.Models.Pages.ProjectOverviewPage_DynamicProxy.MakeReadOnly() +167
   EPiServer.DataAccess.Internal.ContentDB.FinalizeContent(IContent content) +291
   EPiServer.DataAccess.Internal.ContentLoadDB.LoadContentInternal(ContentReference contentLink, Int32 languageBranchId, DbDataReader reader) +1507
   EPiServer.DataAccess.Internal.<>c__DisplayClass4_0.<LoadVersion>b__0() +465
   EPiServer.Data.Providers.Internal.<>c__DisplayClass31_0`1.<Execute>b__0() +58
   EPiServer.Data.Providers.SqlTransientErrorsRetryPolicy.Execute(Func`1 method) +70
   EPiServer.Core.<>c__DisplayClass127_0.<LoadContentFromCacheOrRepository>b__0() +35
   EPiServer.Framework.Cache.ObjectInstanceCacheExtensions.ReadThroughWithWait(IObjectInstanceCache cache, String cacheKey, Func`1 readValue, Func`2 evictionPolicy) +1035
   EPiServer.Core.Internal.ContentInstanceCache.ReadThrough(ContentReference contentLink, String selectedLanguage, Func`1 readValue) +107
   EPiServer.Core.ContentProvider.LoadContentFromCacheOrRepository(ContentReference contentreference, ILanguageSelector selector) +618
   EPiServer.Core.Internal.ProviderPipelineImplementation.GetItem(ContentProvider provider, ContentReference contentLink, LoaderOptions loaderOptions) +327
   EPiServer.Core.Internal.DefaultContentLoader.TryGet(ContentReference contentLink, LoaderOptions loaderOptions, T& content) +577
   EPiServer.Core.Internal.DefaultContentLoader.Get(ContentReference contentLink, LoaderOptions loaderOptions) +99
   ...

Our property is defined as:

[Display(
    Name = "Text",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
[CultureSpecific]
public virtual XhtmlString Text { get; set; }

A quick lookup in the database shows that the the property is indeed defined as an XHtml property:

Does anybody know why this error occurs?

Note: We recently upgraded 11.17 to 11.20, but it is not clear when these problems actually started, we only found out just now.

#265361
Edited, Oct 20, 2021 12:15
Vote:
 

The problem is solved.

It appeared that another property had a wrong type set in the database. This property was of type string in code but had propertyDefinitionID 30 assigned, which is XHtmlString. We manually changed this to 7 (LongString) in the database and restarted EPiServer.

This probleem is strange because of multiple reasons:

  • When attaching a debugger, the last property fed to EPiServer.DataAbstraction.RuntimeModel.Internal.ContentModelMethods.GetValueOrDefault was Text. I concluded this because it failed directly after that call. In retrospect it did not have anything to do with Text.
  • Normally, EPiServer checks for changes that need to be applied to the content definitions and updates the database. It's not clear to me how the EPiServer dit nog set the property definition to that of LongString (7) or String (6) by itself, even after multiple restarts.

But hey, case closed :-)

#265396
Oct 21, 2021 14:06
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.