ExpressionParameterVisitor selects wrong CustomType for ConstantExpression (Linq)

Description

We have a legacy oracle database that uses double values as primary keys. In order to convert those double values to culture-invariant strings we are using a custom "DoubleStringUserType" (the returned type is "typeof(string)").

We have tried to upgrade from NHibernate 4.0.4 to 4.1.0 and now some queries fail because NHibernate 4.1.0 takes this custom DoubleStringUserType for all string constants in the expression.

The trouble is caused by the code introduced in the attached commit:
The Method VisitConstantExpression (ExpressionParameterVisitor) takes the first custom type it finds where the ReturnedType is assignable to the expression.

A simple expression like the following now fails:
repository.Where(p => p.Employee.Id == employeeId && pAddress.Tags.Any(v => v.Keyword== "Employee"))

because the ExpressionParameterVisitor in NHibernate 4.1 takes the constant expression "Employee" of type string and the visitor finds the first custom type assignable to string (which happens to be our DoubleStringUserType). So eventually NHibernate now tries to convert the string "Employee" to a double (using our custom user type) and this causes an invalid cast exception (for obvious reasons).

I've cloned the NHibernate repo (commit 36100cc0a31f85e2364171839904575fb649473f) and commented out the following lines and then our queries start to work again:

1 2 3 4 5 6 7 8 if (type == null) { var customType = _allMappedCustomTypes.FirstOrDefault(ct => ct.UserType.ReturnedType.IsAssignableFrom(expression.Type)); if (customType != null) { type = customType; } }

The code snippet is taken from the fix for this issue:
https://nhibernate.jira.com/browse/NH-3904

I hope that my problem description is detailed enough (if you need more infos let me know).

Environment

None

Status

Assignee

Alex Zaytsev

Reporter

Marcel Studer

Components

Fix versions

Affects versions

4.1.0

Priority

Blocker
Configure