StartsWith / EndsWith / Contains don't use correct AnsiString type
Description
Environment
Attachments
Activity

Paulj June 9, 2017 at 4:25 PM
Yes, thanks for your help Frederic

Frédéric Delaporte June 9, 2017 at 2:59 PMEdited
As written by Alexander on this PR upon your request, MappedAs
can already specify the length.

Frédéric Delaporte June 9, 2017 at 2:45 PMEdited
First, does the parameter length causes any performance issue as the parameter type do? Maybe is it not worth it to adjust it.
Then, parameters in Linq queries are automatically extracted from literals found in the lambda. There are no places to adjust the resulting DbParameter
, excepted with the MappedAs
extension. So if you want some way to set a parameter length, the best place is currently to check how MappedAs
works then add to it an overload taking the length and do required changes for having it working. MappedAs
is processed in an expression tree visitor which detect it by reflection, so to find that in NHibernate sources, search it by name, not just by references.
If you want to add this functionality, please add a new Jira issue, since it would not be a fix but a new functionality giving a workaround. And check contributing guidelines.

Paulj June 9, 2017 at 12:29 PM
Thanks again...
Do you known any way to set type/length in a Linq query Contains?
I am willing to modify the source to do that, but I dont known where I should look,

Frédéric Delaporte June 8, 2017 at 7:34 PMEdited
MappedAs
does not currently allow to specify the type length/precision/scale. Maybe this could be a new feature, which would add some MappedAs
overload for this.
MappedAs
is for adjusting the type of what is converted to a query parameter: it does not try to infer anything from "nearby" entities. So those entities mappings are not taken into account for setting the parameter type characteristics, and this is by design.
If I have a mapping with a property of type
AnsiString
:and I query:
NH translates this into
but @p0 is defined as
string(nvarchar)
instead ofAnsiString(varchar)
, which gives terrible performance in the DB Engine as the types don't match.Equality and other operators mantain correct type. Doing this query with QueryOver also allows correct type usage.
I see in code that the
StartsWithGenerator
, as well as theEndsWithGenerator
andContainsGenerator
, usesConcat
, that seems to be the cause of the problem, but I'm a bit lost here...