NHibernate将字符串参数转换为nvarchar而不是varchar。我该如何阻止呢?

时间:2021-06-04 10:00:40

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.

我有一个类映射到一个视图,并且正在搜索第一个和最后一个名称,以便搜索患者记录。该视图最终查看患者表上的第一个和最后一个名称字段(可能还有其他字段,取决于输入)。当标准转换为SQL时,它将输入我的字符串作为nvarchar参数。我已经在我的映射上使用了type="AnsiString"和length="50",但它仍然将这些转换为nvarchar,这将导致我的查询性能受到影响。

  <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.

SQL Profiler显示了使用nvarchar参数的输出SQL,并将我的所有字符串都预先设置为N。

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.

我遗漏了什么东西?在标准或映射上还有什么需要做的吗?此外,参数的长度也不是常数50。我使用的是NHibernate 2.1。

2 个解决方案

#1


2  

Try using the sql-type attribute in your mappings:

尝试在映射中使用sql类型属性:

<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.

你有什么性能问题?在我看来,把字符串当作unicode来处理会对性能有很大的影响。

#2


2  

Try this

试试这个

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

#1


2  

Try using the sql-type attribute in your mappings:

尝试在映射中使用sql类型属性:

<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.

你有什么性能问题?在我看来,把字符串当作unicode来处理会对性能有很大的影响。

#2


2  

Try this

试试这个

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