简体   繁体   English

为什么 NHibernate 使用 NVARCHAR 而不是 VARCHAR 忽略列类型映射?

[英]Why NHibernate uses NVARCHAR instead of VARCHAR ignoring column type mapping?

When I run the following code:当我运行以下代码时:

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

NHibernate generates the following SQL query: NHibernate 生成以下 SQL 查询:

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

The problem here is the CAST to nvarchar(4000) .这里的问题是 CAST 到nvarchar(4000) The hbm.xml file defines the mapping for the object_id column as: hbm.xml 文件将object_id列的映射定义为:

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

So why NHibernate is ignoring the information in the mapping file and not using varchar(40) ?那么为什么 NHibernate 会忽略映射文件中的信息而不使用varchar(40)呢? I could not find a way of explicitly stating the property type used for the criteria.我找不到明确说明用于标准的属性类型的方法。 I'm not even sure if I'd need it, the mapping is in the hbm.xml file, why won't NHibernate pick it up?我什至不确定我是否需要它,映射在 hbm.xml 文件中,为什么 NHibernate 不提取它?

This is NHibernate 4.1.4000 running on .Net Framework 4.6 (legacy app).这是运行在 .Net Framework 4.6(旧版应用)上的 NHibernate 4.1.4000。

While mapping the property in your code:在您的代码中映射属性时:

<property name="Object_id" type="String">

you are specifying type as String .您将type指定为String Change it to AnsiString instead.改为将其更改为AnsiString

Please refer to NHibernate Documentation and search for "Table 5.3. System.ValueType Mapping Types" in there for list of all types.请参阅NHibernate 文档并在其中搜索“表 5.3. System.ValueType 映射类型”以获取所有类型的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM