diff --git a/src/Umbraco.Core/Persistence/Factories/MemberTypeReadOnlyFactory.cs b/src/Umbraco.Core/Persistence/Factories/MemberTypeReadOnlyFactory.cs index 7fe64a3f33..e291916e10 100644 --- a/src/Umbraco.Core/Persistence/Factories/MemberTypeReadOnlyFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/MemberTypeReadOnlyFactory.cs @@ -94,7 +94,10 @@ namespace Umbraco.Core.Persistence.Factories var propertyType = new PropertyType(typeDto.ControlId, //ensures that any built-in membership properties have their correct dbtype assigned no matter //what the underlying data type is - MemberTypeRepository.GetDbTypeForProperty(typeDto.Alias, typeDto.DbType.EnumParse(true), standardProps)) + MemberTypeRepository.GetDbTypeForBuiltInProperty( + typeDto.Alias, + typeDto.DbType.EnumParse(true), + standardProps).Result) { Alias = typeDto.Alias, DataTypeDefinitionId = typeDto.DataTypeId, diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs index fb700e4f30..4d0e925461 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs @@ -145,8 +145,7 @@ namespace Umbraco.Core.Persistence.Repositories //If the Id of the DataType is not set, we resolve it from the db by its ControlId if (propertyType.DataTypeDefinitionId == 0 || propertyType.DataTypeDefinitionId == default(int)) { - var datatype = Database.FirstOrDefault("WHERE controlId = @Id", new { Id = propertyType.DataTypeId }); - propertyType.DataTypeDefinitionId = datatype.DataTypeId; + AssignDataTypeFromPropertyEditor(propertyType); } var propertyTypeDto = propertyFactory.BuildPropertyTypeDto(tabId, propertyType); int typePrimaryKey = Convert.ToInt32(Database.Insert(propertyTypeDto)); @@ -299,24 +298,11 @@ namespace Umbraco.Core.Persistence.Repositories foreach (var propertyType in entity.PropertyTypes) { var tabId = propertyType.PropertyGroupId != null ? propertyType.PropertyGroupId.Value : default(int); + //If the Id of the DataType is not set, we resolve it from the db by its ControlId if (propertyType.DataTypeDefinitionId == 0 || propertyType.DataTypeDefinitionId == default(int)) { - //we cannot try to assign a data type of it's an empty guid - if (propertyType.DataTypeId != Guid.Empty) - { - var sql = new Sql() - .Select("*") - .From() - .Where("controlId = @Id", new { Id = propertyType.DataTypeId }) - .OrderBy(typeDto => typeDto.DataTypeId); - var datatype = Database.FirstOrDefault(sql); - //we cannot assign a data type if one was not found - if (datatype != null) - { - propertyType.DataTypeDefinitionId = datatype.DataTypeId; - } - } + AssignDataTypeFromPropertyEditor(propertyType); } //validate the alias! @@ -433,5 +419,32 @@ namespace Umbraco.Core.Persistence.Repositories throw exception; }); } + + /// + /// Try to set the data type id based on its ControlId + /// + /// + private void AssignDataTypeFromPropertyEditor(PropertyType propertyType) + { + //we cannot try to assign a data type of it's an empty guid + if (propertyType.DataTypeId != Guid.Empty) + { + var sql = new Sql() + .Select("*") + .From() + .Where("controlId = @Id", new { Id = propertyType.DataTypeId }) + .OrderBy(typeDto => typeDto.DataTypeId); + var datatype = Database.FirstOrDefault(sql); + //we cannot assign a data type if one was not found + if (datatype != null) + { + propertyType.DataTypeDefinitionId = datatype.DataTypeId; + } + else + { + LogHelper.Warn>("Could not assign a data type for the property type " + propertyType.Alias + " since no data type was found with a property editor " + propertyType.DataTypeId); + } + } + } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/MemberTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/MemberTypeRepository.cs index 8ecc194d29..0fa8811d26 100644 --- a/src/Umbraco.Core/Persistence/Repositories/MemberTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/MemberTypeRepository.cs @@ -245,10 +245,14 @@ namespace Umbraco.Core.Persistence.Repositories var stdProps = Constants.Conventions.Member.GetStandardPropertyTypeStubs(); foreach (var propertyType in memberType.PropertyTypes) { - propertyType.DataTypeDatabaseType = GetDbTypeForProperty(propertyType.Alias, propertyType.DataTypeDatabaseType, stdProps); - //this reset's it's current data type reference which will be re-assigned based on the property editor assigned on the next line - propertyType.DataTypeDefinitionId = 0; - propertyType.DataTypeId = GetPropertyEditorForProperty(propertyType.Alias, propertyType.DataTypeId, stdProps); + var dbTypeAttempt = GetDbTypeForBuiltInProperty(propertyType.Alias, propertyType.DataTypeDatabaseType, stdProps); + if (dbTypeAttempt) + { + propertyType.DataTypeDatabaseType = dbTypeAttempt.Result; + //this reset's it's current data type reference which will be re-assigned based on the property editor assigned on the next line + propertyType.DataTypeDefinitionId = 0; + propertyType.DataTypeId = GetPropertyEditorForBuiltInProperty(propertyType.Alias, propertyType.DataTypeId, stdProps).Result; + } } } @@ -273,8 +277,10 @@ namespace Umbraco.Core.Persistence.Repositories /// /// /// - /// - internal static DataTypeDatabaseType GetDbTypeForProperty( + /// + /// Successful attempt if it was a built in property + /// + internal static Attempt GetDbTypeForBuiltInProperty( string propAlias, DataTypeDatabaseType dbType, Dictionary standardProps) @@ -285,13 +291,22 @@ namespace Umbraco.Core.Persistence.Repositories if (aliases.Contains(propAlias)) { //return the pre-determined db type for this property - return standardProps.Single(x => x.Key == propAlias).Value.DataTypeDatabaseType; + return Attempt.Succeed(standardProps.Single(x => x.Key == propAlias).Value.DataTypeDatabaseType); } - return dbType; + return Attempt.Fail(dbType); } - internal static Guid GetPropertyEditorForProperty( + /// + /// + /// + /// + /// + /// + /// + /// Successful attempt if it was a built in property + /// + internal static Attempt GetPropertyEditorForBuiltInProperty( string propAlias, Guid propertyEditor, Dictionary standardProps) @@ -302,10 +317,10 @@ namespace Umbraco.Core.Persistence.Repositories if (aliases.Contains(propAlias)) { //return the pre-determined db type for this property - return standardProps.Single(x => x.Key == propAlias).Value.DataTypeId; + return Attempt.Succeed(standardProps.Single(x => x.Key == propAlias).Value.DataTypeId); } - return propertyEditor; + return Attempt.Fail(propertyEditor); } } } \ No newline at end of file