4

I have a nvarchar(2000) column in a SQL Server 2005 database, and have mapped this into NHibernate as:

<property name="Query" column="`Query`" type="String" length="2000" not-null="false"/>

The DTO class just presents a string property:

    public virtual string Query { get; set; }

If I set the query to a string of > 2000 characters I get an exception from SQL server to the effect of:

"String or binary data would be truncated. The statement has been terminated."

What I want is for this truncation to just happen automatically and silently. I could override the virtual property and force the truncation on the property set, but feel there should be a way to get this behaviour by default, either via the NHibernate mapping or even as a SQL server setting.

Am I missing anything ... I don't want to change the db schema to allow longer strings.

1 Answer 1

4

Create a custom User Type and which truncates the string if it's longer than 2000 chars. Here is sample of creating User Type

<property name="Query" type="Common.Nhibernate.Types.StringTruncType, Common" column="`Query`" type="String" length="2000" not-null="false"/>

Not the answer you're looking for? Browse other questions tagged or ask your own question.