StartsWith / EndsWith / Contains don't use correct AnsiString type

Description

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 of AnsiString(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 the EndsWithGenerator and ContainsGenerator, uses Concat, that seems to be the cause of the problem, but I'm a bit lost here...

Environment

None

Attachments

1

Activity

Show:

Paulj June 9, 2017 at 4:25 PM

Yes, thanks for your help Frederic

Frédéric Delaporte June 9, 2017 at 2:59 PM
Edited

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 PM
Edited

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 PM
Edited

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.

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?

Created November 4, 2013 at 7:25 PM
Updated June 9, 2017 at 4:25 PM
Who's Looking?