3

typically definition of property/column looks like this:

<property name="DocumentSummary" column="DocumentSummary" length="100" type="String">
   <column name="DocumentSummary" />
</property>

I was assuming that NHibernate will validate data using length attribute before inserting/updating a row. Unfortunately it seems to be invalid assumption. So question is if NHibernate is using length attribute for anything? Does it make sense to set it for each property?

Marek Blotny
  • 1,503
  • 1
  • 9
  • 7

3 Answers3

4

It using it to create database definition sql. Look at SchemaExport class.

Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
1

Also you might be able to programmically pull the lengths from the mappings, see this question. Note that that is for Java Hibernate, but I believe you might be able to translate that to NHibernate.

Community
  • 1
  • 1
James McMahon
  • 48,506
  • 64
  • 207
  • 283
0

Yes, setting the Length property makes a different where you have string columns with a length that exceeds the default maximum length.

We have a column of type NVARCHAR(MAX). If we didn't set the length property, NHibernate would fail to set the string value if the value had a length greater than 4096 characters.

(This example uses Mapping-By-Code, because we're in 2017 :-)

classMapper.Property(
                        tickerFeed => tickerFeed.StaticTickerText,
                        propertyMapper =>
                        {
                             propertyMapper.Column("StaticTickerText");
                             propertyMapper.Length(int.MaxValue);
                        }
                    );

However, we found we could achieve the same result using IPropertyMapper.Type to stipulate that it was a large string.

classMapper.Property(
                        tickerFeed => tickerFeed.StaticTickerText,
                        propertyMapper =>
                        {
                             propertyMapper.Column("StaticTickerText");
                             propertyMapper.Type(NHibernateUtil.StringClob);
                        }
                    );

We ended up going with the propertyMapper.Type option, as it is more explicit.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205