Search code examples
nhibernatenhibernate-mappingnhibernate-criteria

NHibernate converting string parameters to nvarchar instead of varchar. How can I stop this?


I have a class mapped to a view and am searching on first and last names in order to search for patient records. The view ultimately looks at the first and last name fields on the patient table (possibly others as well depending on input). When the criteria converts to SQL, it's entering my strings as nvarchar parameters. I've already used type="AnsiString" and length="50" on my mapping, but it still is converting these to nvarchar, which is causing a performance hit on my query.

  <class name="PatientSearchResult" table="vw_PatientSearch" mutable="false" >
    <id name="Id" type="Guid" column="PatientId"/>

    <property name="MedicalRecordNumber" type="AnsiString" length="50" />
    <property name="Title" />
    <property name="FirstName" type="AnsiString" length="50" />
    <property name="MiddleName" />
    <property name="LastName" type="AnsiString" length="50" />
    <property name="Nickname" />
    <property name="Suffix" />
    <property name="DateOfBirth" />
    <property name="IsRestricted" />
    <property name="IsDeleted" />

    <component name="Address">
      <property name="StreetAddress1" />
      <property name="StreetAddress2" />
      <property name="City" />
      <property name="State" />
      <property name="PostalCode" />
    </component>

  </class>

SQL Profiler is showing the output SQL as using nvarchar parameters and prefixing all of my strings with N to cast them.

Am I missing something? Is there anything else that needs to be done on the criteria or the mapping? Additionally, the length of the parameters is not a constant 50 either. I am using NHibernate 2.1.


Solution

  • Try using the sql-type attribute in your mappings:

    <property name="FirstName" length="50" />
        <column sql-type="varchar(50)" />
    </property>
    

    What performance issues are you having? It seems unlikely to me that treating strings as unicode would affect performance all that much.