【问题标题】:How to set type of parameter in linq to nhibernate query for char(*) type?如何将 linq 中的参数类型设置为 char(*) 类型的休眠查询?
【发布时间】:2016-11-30 04:02:34
【问题描述】:

我有一个按 char(4) 类型在 Person 表中的 Code 列。

人表是:

人 Hbm 是:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain.Model">      
  <class name="Person" table="Person">    

     <property name="Code" >      
        <column name="Code" **sql-type="char(4)"**/>    
     </property>

     ...

  </class>
</hibernate-mapping>

现在我有一个 linq to nhibernate 查询:

SessionInstance.Query<Person>()
               .Where(x => x.Code == "1234")
               .Select(x => x.Code)
               .ToList();

但不希望在 sql profiler 中跟踪查询。

exec sp_executesql N'select person_.Code as col_0_0_ from Person person_ where person_.Code=@p0',N'@p0 nvarchar(4000)',@p0=N'1234'

因为@p0 的类型不是char(4)@p0 的值是N'1234' 而不是'1234'

而且此查询的性能较低:

什么原因?

【问题讨论】:

    标签: c# sql linq nhibernate profiler


    【解决方案1】:

    省略显式类型声明应该完全没问题。通常,您需要定义数据类型的唯一场景是 NHibernate 不默认为您打算使用的数据类型(常见于日期/时间数据类型的某些变体)。

    【讨论】:

      【解决方案2】:

      这是 NHibernate 4.0 中的一个已知限制: https://nhibernate.jira.com/browse/NH-3403

      【讨论】:

        猜你喜欢
        • 2012-10-29
        • 2011-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        相关资源
        最近更新 更多