You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a table with a primary-key ID [int]. The corresponding csharp class has a field ID, which is an enum [EnumTest:int]. If I make an insert on this table, then I get the exception "Compiler.Entity/Object.Property: Failed to automatically convert the value expression..."
The problem seems to be in Compiler.cs. In cases of Global-Option "Automatic" or when db-field is a primary-key it uses the "auto-conversion-handling", which doesn't cover enums. In this code-area it throws the exception. The code-block after would handle the enum-conversion, but to late:
Origin Code: Compiler.cs
internalstaticExpressionGetEntityInstancePropertyValueExpression(ExpressionentityInstanceExpression,ClassPropertyclassProperty,DbFielddbField){varexpression=(Expression)Expression.Property(entityInstanceExpression,classProperty.PropertyInfo);// Target typevarhandlerInstance=classProperty.GetPropertyHandler()??PropertyHandlerCache.Get<object>(dbField.Type.GetUnderlyingType());vartargetType=GetPropertyHandlerSetParameter(handlerInstance)?.ParameterType??dbField.Type;// Auto-conversion Handlingif(GlobalConfiguration.Options.ConversionType==ConversionType.Automatic||dbField?.IsPrimary==true||dbField?.IsIdentity==true){try{//THIS LINE FAILSexpression=ConvertExpressionWithAutomaticConversion(expression,targetType?.GetUnderlyingType());}catch(Exceptionex){thrownewInvalidOperationException($"Compiler.Entity/Object.Property: Failed to automatically convert the value expression for "+$"property '{classProperty.GetMappedName()} ({classProperty.PropertyInfo.PropertyType.FullName})'. {classProperty}",ex);}}/* * Note: The other data provider can coerce the Enum into its destination data type in the DB by default, * except for PostgreSQL. The code written below is only to address the issue for this specific provider. */// Enum Handlingif(classProperty.PropertyInfo.PropertyType.GetUnderlyingType().IsEnum==true){try{if(!IsPostgreSqlUserDefined(dbField)){vardbType=classProperty.GetDbType()??classProperty.PropertyInfo.PropertyType.GetUnderlyingType().GetDbType();vartoType=dbType.HasValue?newDbTypeToClientTypeResolver().Resolve(dbType.Value):targetType?.GetUnderlyingType();expression=ConvertEnumExpressionToTypeExpression(expression,toType);}}catch(Exceptionex){thrownewInvalidOperationException($"Compiler.Entity/Object.Property: Failed to convert the value expression from "+$"enumeration '{classProperty.PropertyInfo.PropertyType.FullName}' to type '{targetType?.GetUnderlyingType()}'. {classProperty}",ex);}}// Property Handlertry{expression=ConvertExpressionToPropertyHandlerSetExpression(expression,null,classProperty,dbField?.Type.GetUnderlyingType());}catch(Exceptionex){thrownewInvalidOperationException($"Compiler.Entity/Object.Property: Failed to convert the value expression for property handler '{handlerInstance?.GetType()}'. "+$"{classProperty}",ex);}// Return the ValuereturnConvertExpressionToTypeExpression(expression,StaticType.Object);}
Possible fix: Compiler.cs
I think it's better to check for enum before doing auto-conversion.
internalstaticExpressionGetEntityInstancePropertyValueExpression(ExpressionentityInstanceExpression,ClassPropertyclassProperty,DbFielddbField){varexpression=(Expression)Expression.Property(entityInstanceExpression,classProperty.PropertyInfo);// Target typevarhandlerInstance=classProperty.GetPropertyHandler()??PropertyHandlerCache.Get<object>(dbField.Type.GetUnderlyingType());vartargetType=GetPropertyHandlerSetParameter(handlerInstance)?.ParameterType??dbField.Type;/* * Note: The other data provider can coerce the Enum into its destination data type in the DB by default, * except for PostgreSQL. The code written below is only to address the issue for this specific provider. */// Enum Handlingif(classProperty.PropertyInfo.PropertyType.GetUnderlyingType().IsEnum==true&&!IsPostgreSqlUserDefined(dbField)){try{vardbType=classProperty.GetDbType()??classProperty.PropertyInfo.PropertyType.GetUnderlyingType().GetDbType();vartoType=dbType.HasValue?newDbTypeToClientTypeResolver().Resolve(dbType.Value):targetType?.GetUnderlyingType();expression=ConvertEnumExpressionToTypeExpression(expression,toType);}catch(Exceptionex){thrownewInvalidOperationException($"Compiler.Entity/Object.Property: Failed to convert the value expression from "+$"enumeration '{classProperty.PropertyInfo.PropertyType.FullName}' to type '{targetType?.GetUnderlyingType()}'. {classProperty}",ex);}}elseif(GlobalConfiguration.Options.ConversionType==ConversionType.Automatic||dbField?.IsPrimary==true||dbField?.IsIdentity==true){// Auto-conversion Handlingtry{expression=ConvertExpressionWithAutomaticConversion(expression,targetType?.GetUnderlyingType());}catch(Exceptionex){thrownewInvalidOperationException($"Compiler.Entity/Object.Property: Failed to automatically convert the value expression for "+$"property '{classProperty.GetMappedName()} ({classProperty.PropertyInfo.PropertyType.FullName})'. {classProperty}",ex);}}// Property Handlertry{expression=ConvertExpressionToPropertyHandlerSetExpression(expression,null,classProperty,dbField?.Type.GetUnderlyingType());}catch(Exceptionex){thrownewInvalidOperationException($"Compiler.Entity/Object.Property: Failed to convert the value expression for property handler '{handlerInstance?.GetType()}'. "+$"{classProperty}",ex);}// Return the ValuereturnConvertExpressionToTypeExpression(expression,StaticType.Object);}
The text was updated successfully, but these errors were encountered:
Wierd! Pretty sure this has been working in the past, but someway along the road, this bug has been introduced and is not captured by the Integration Test. FYI: As of writing this, this error is also happening even on a simple Automatic conversion use-case of enum.
Problem
I have a table with a primary-key ID [int]. The corresponding csharp class has a field ID, which is an enum [EnumTest:int]. If I make an insert on this table, then I get the exception "Compiler.Entity/Object.Property: Failed to automatically convert the value expression..."
The problem seems to be in Compiler.cs. In cases of Global-Option "Automatic" or when db-field is a primary-key it uses the "auto-conversion-handling", which doesn't cover enums. In this code-area it throws the exception. The code-block after would handle the enum-conversion, but to late:
Origin Code: Compiler.cs
Possible fix: Compiler.cs
I think it's better to check for enum before doing auto-conversion.
The text was updated successfully, but these errors were encountered: