NHibernate将字符串参数转换为nvarchar而不是varchar.我该如何阻止呢? [英] NHibernate converting string parameters to nvarchar instead of varchar. How can I stop this?

查看:233
本文介绍了NHibernate将字符串参数转换为nvarchar而不是varchar.我该如何阻止呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射到视图的类,并且正在搜索名字和姓氏以搜索患者记录.该视图最终查看患者表上的名字和姓氏字段(视输入而定,也可能是其他字段).当条件转换为SQL时,它将作为nvarchar参数输入我的字符串.我已经在映射中使用了type ="AnsiString"和length ="50",但仍将其转换为nvarchar,这对我的查询造成了性能损失.

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事件探查器显示输出SQL使用nvarchar参数,并用N前缀对所有字符串进行强制转换.

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

我错过了什么吗?在条件或映射上还有其他需要做的事情吗?此外,参数的长度也不是恒定的50.我正在使用NHibernate 2.1.

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.

推荐答案

尝试在映射中使用sql-type属性:

Try using the sql-type attribute in your mappings:

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

您遇到什么性能问题?在我看来,将字符串视为unicode似乎不太会影响性能.

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

这篇关于NHibernate将字符串参数转换为nvarchar而不是varchar.我该如何阻止呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆