简体   繁体   English

NHibernate:为查询参数显式设置数据类型和长度

[英]NHibernate: Setting datatypes and lengths explicitly for query parameters

I am using NHibernate mapping by code and sql server 2012. I am also using the Sql2008ClientDriver as the driver in the session factory. 我正在通过代码和sql server 2012使用NHibernate映射。我还将Sql2008ClientDriver用作会话工厂中的驱动程序。 I have been trying to figure out how to map the exact sql type and length to the C# objects representing the table. 我一直在尝试找出如何将确切的sql类型和长度映射到代表表的C#对象。 The problem I am getting is around the data types specified in the sql generated, specifically strings. 我遇到的问题是围绕生成的sql中指定的数据类型,特别是字符串。

For example: 例如:

public virtual string SomeProperty { get; 公共虚拟字符串SomeProperty {get; set; 组; } }

This translates to a nvarchar(4000) parameter when the query is generated, but this column is a char(6) in the database. 生成查询时,这将转换为nvarchar(4000)参数,但是此列是数据库中的char(6)。 Is it possible to specify this some how in the mapping? 是否可以在映射中指定此方式?

I believe there is a performance loss, as sql server is doing a conversion before executing the query. 我相信会有性能损失,因为sql Server在执行查询之前正在执行转换。

I've tried this (and it does not work) : http://notherdev.blogspot.com.au/2012/01/mapping-by-code-property.html 我已经尝试过此方法(它不起作用): http : //notherdev.blogspot.com.au/2012/01/mapping-by-code-property.html

Any ideas? 有任何想法吗?

Here's a loquacious mapping that results in a working char(6) field. 这是一个过时的映射,导致char(6)字段正常工作。

Property(x => x.SomeProperty,
         pm =>
         {
             pm.Type(NHibernateUtil.AnsiString);
             pm.Length(6);
             pm.Column(cm => cm.SqlType("char(6)"));
         });

Out of the box support for this could be better. 开箱即用的支持可能会更好。

In your mapping file you can specify the sql-type : 在映射文件中,您可以指定sql-type

<property name="SomeProperty" type="String">
   <column name="ColumnName" sql-type="char(6)" />
</property>     

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

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