2
votes

I am using NHibernate mapping by code and sql server 2012. I am also using the Sql2008ClientDriver as the driver in the session factory. I have been trying to figure out how to map the exact sql type and length to the C# objects representing the table. The problem I am getting is around the data types specified in the sql generated, specifically strings.

For example:

public virtual string 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. 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.

I've tried this (and it does not work) : http://notherdev.blogspot.com.au/2012/01/mapping-by-code-property.html

Any ideas?

2

2 Answers

1
votes

Here's a loquacious mapping that results in a working char(6) field.

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.

0
votes

In your mapping file you can specify the sql-type:

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