advanced-cms / time-property

Episerver property used to store time between 0-24 hours
Apache License 2.0
0 stars 0 forks source link

CMS 12 upgraded solution throws error from ModelSyncInitialization on startup #9

Open krompaco opened 1 year ago

krompaco commented 1 year ago

Hi!

I have a database that was upgraded from CMS 11 and 1.0.0 version.

I believe 3.0.1 was not configured correctly on first startup if could matter but I have 3.0.1 and properties in one local block that looks like this.

[BackingType(typeof(Advanced.CMS.TimeProperty.TimeProperty))]
public virtual TimeSpan? OpeningHour { get; set; }

[BackingType(typeof(Advanced.CMS.TimeProperty.TimeProperty))]
public virtual TimeSpan? ClosingHour { get; set; }

[BackingType(typeof(Advanced.CMS.TimeProperty.TimeProperty))]
public virtual TimeSpan? LunchStart { get; set; }

[BackingType(typeof(Advanced.CMS.TimeProperty.TimeProperty))]
public virtual TimeSpan? LunchEnd { get; set; }

I have services.AddTimeProperty(); in Startup.cs and when I start under these conditions I get these errors logged and app fails to start.

warn: EPiServer.Construction.Internal.PropertyDataFactory[0]
      Unable to create a PropertyData instance of Type: 'AdvancedCms.TimeProperty.TimeProperty' Assembly: 'AdvancedCms.TimeProperty'. Will fallback using the data type instead.
fail: EPiServer.Framework.Cache.ObjectInstanceCacheExtensions[0]
      Failed to Read cacheKey = 'EPContentVersion:25046_52145'
      System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Nullable`1[System.TimeSpan]'.
         at EPiServer.DataAbstraction.RuntimeModel.Internal.ContentModelMethods.GetValueOrDefault[T](PropertyDataCollection propertyDataCollection, String propertyName, Boolean defaultOnIsNull)

If I comment out the properties and Startup-line the site starts correctly.

Any ideas on what I can try?

gregwiechec commented 1 year ago

Very strange issue. Does this not working only for nullable TimeSpans (TimeSpan?) or for non-nullable too (TimeSpan)?

krompaco commented 1 year ago

Tried removing the ?-marks. Still get a similar error:

warn: EPiServer.Construction.Internal.PropertyDataFactory[0]
      Unable to create a PropertyData instance of Type: 'AdvancedCms.TimeProperty.TimeProperty' Assembly: 'AdvancedCms.TimeProperty'. Will fallback using the data type instead.
fail: EPiServer.Framework.Cache.ObjectInstanceCacheExtensions[0]
      Failed to Read cacheKey = 'EPContentVersion:25046_52145'
      System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.TimeSpan'.

Also tried with that in place on a non-upgraded database with same result.

Console shows this so it there is a detection of that a change is needed, is this correct?

peModelRegister.ValidateChangeOfModelType(PropertyDefinitionModel propertyModel, String modelName)
         at EPiServer.DataAbstraction.RuntimeModel.Internal.ContentTypeModelRegister.SetStateForPropertyDefinitionModels(ContentTypeModel model)
         at EPiServer.DataAbstraction.RuntimeModel.Internal.ContentTypeModelRegister.<AnalyzeProperties>b__15_0(ContentTypeModel model)
krompaco commented 1 year ago

I see what the problem is in this code base... I could spot it by comparing databases and deleting all values.

SELECT * FROM tblContentProperty WHERE fkPropertyDefinitionID IN (SELECT pkID FROM tblPropertyDefinition WHERE fkPropertyDefinitionTypeID = 1078)
SELECT * FROM tblWorkContentProperty WHERE fkPropertyDefinitionID IN (SELECT pkID FROM tblPropertyDefinition WHERE fkPropertyDefinitionTypeID = 1078)

-- See what happens if I delete
DELETE FROM tblContentProperty WHERE fkPropertyDefinitionID IN (SELECT pkID FROM tblPropertyDefinition WHERE fkPropertyDefinitionTypeID = 1078)
DELETE FROM tblWorkContentProperty WHERE fkPropertyDefinitionID IN (SELECT pkID FROM tblPropertyDefinition WHERE fkPropertyDefinitionTypeID = 1078)

-- After starting up (successfully) I now find the values I add on a new fkPropertyDefinitionTypeID
SELECT * FROM tblContentProperty WHERE fkPropertyDefinitionID IN (SELECT pkID FROM tblPropertyDefinition WHERE fkPropertyDefinitionTypeID = 1105)
SELECT * FROM tblWorkContentProperty WHERE fkPropertyDefinitionID IN (SELECT pkID FROM tblPropertyDefinition WHERE fkPropertyDefinitionTypeID = 1105)

Showing the difference between where the values were put between databases and after deleting for 12-db I see that I have a namespace name change I need to handle.

image

Any advice on how to change from "AdvancedCms.TimeProperty" to "Advanced.CMS.TimeProperty"?

barteksekula commented 1 year ago

Oh... I guess a manual sql update script would be needed. We changed the namespace between .net framework and .net core so it was a breaking change release.

krompaco commented 1 year ago

For reference I ran this from Startup. Seems to have done the job.

UPDATE tblPropertyDefinitionType SET TypeName = 'Advanced.CMS.TimeProperty.TimeProperty', AssemblyName = 'Advanced.CMS.TimeProperty' WHERE AssemblyName = 'AdvancedCms.TimeProperty'