Description
Hello,
we are currently updating from an old NHibernate 4 version to the current NHibernate 5 (5.2.3) version and are facing some issues.
One of our models contains a nullable string column which is joined on a column with a different type of an other table by using ToString() within the join, e.g:
q =
from o in db.Orders
join s in db.Shippers
on o.ShippedTo equals s.Reference.ToString()
where o.ShippedTo != null
select o.OrderId;
This worked in NHibernate 4 but fails in NHibernate 5. Here is a logged query example from our production code with
Join-Exception.txt
NHibernate 4 and NHibernate 5.
-- NHibernate 4
select invruletem0_.ID as col_0_0_
from InvRuleTemplate invruletem0_,
InvRule invrule1_
where (invruletem0_.Mandator & -1 /* @p0 */) != 0
and (invrule1_.Mandator & -1 /* @p0 */) != 0
and invrule1_.TargetID = cast(invruletem0_.ID as nvarchar(50))
and (invrule1_.TargetID is not null)
-- NHibernate 5
select invruletem0_.ID as col_0_0_
from InvRuleTemplate invruletem0_,
InvRule invrule1_
where (invruletem0_.Mandator & -1 /* @p0 */) != 0
and (invrule1_.Mandator & -1 /* @p0 */) != 0
and invrule1_.TargetID = invruletem0_.ID
and (invrule1_.TargetID is not null)
It looks like that the .ToString() cast isn't translated into SQL anymore. This only happens when the string column is nullable. I guess the query translator recognizes that a cast isn't necessary for not nullable string columns.
This problem is reproducable with a unit test with the northwind domainmodel:
[TestFixture]
public class LinqQuerySamples : LinqTestCase
{
...
[Category("JOIN")]
[Test(Description = "This sample shows how to construct a join where one side is a nullable string and the other side a not nullable guid.")]
public void DLinqJoin11()
{
var q =
from o in db.Orders
join s in db.Shippers
on o.ShippedTo equals s.Reference.ToString()
where o.ShippedTo != null
select o.OrderId;
ObjectDumper.Write(q);
}
....
}
I've also attached the unit test exception.
Thx in advance. Best regards!
Join-Exception.txt