【问题标题】:Why NHibernate uses NVARCHAR instead of VARCHAR ignoring column type mapping?为什么 NHibernate 使用 NVARCHAR 而不是 VARCHAR 忽略列类型映射?
【发布时间】:2021-12-22 08:56:29
【问题描述】:

当我运行以下代码时:

List<Dag> result = session.CreateCriteria<Dag>()
            .Add(Expression.Eq("Object_id", pObjectId))
            .List<Dag>().ToList();
}

NHibernate 生成以下 SQL 查询:

exec sp_executesql N'SELECT this_. ... FROM schm.dag this_ WHERE this_.object_id = @p0',N'@p0 nvarchar(4000)',@p0=N'...'

这里的问题是 CAST 到 nvarchar(4000)。 hbm.xml 文件将object_id 列的映射定义为:

<property name="Object_id" type="String">
  <column name="object_id" not-null="false" length="40" sql-type="varchar" />
</property>

那么为什么 NHibernate 会忽略映射文件中的信息而不使用varchar(40)?我找不到明确说明用于标准的属性类型的方法。我什至不确定我是否需要它,映射在 hbm.xml 文件中,为什么 NHibernate 不接受它?

这是在 .Net Framework 4.6(旧版应用程序)上运行的 NHibernate 4.1.4000。

【问题讨论】:

    标签: c# nhibernate nhibernate-mapping hbm


    【解决方案1】:

    在代码中映射属性时:

    &lt;property name="Object_id" type="String"&gt;

    您将type 指定为String。改为AnsiString

    请参考NHibernate Documentation并在其中搜索“Table 5.3. System.ValueType Mapping Types”以获取所有类型的列表。

    【讨论】:

    • 非常感谢阿米特。情况确实如此。 Nhibernate 现在使用 varchar(8000)
    猜你喜欢
    • 1970-01-01
    • 2014-03-29
    • 2015-11-30
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多