NText size limited to 4000 characters

Topics: Troubleshooting
Jun 10, 2012 at 9:23 AM


I am having a problem with writing a string longer, than 4000 characters into the database, into a column with the type of NText. In Migrations.cs, the column is extended with Unlimited(), and still, an exception is thrown.
I tried modifying the code as it is in, for example BodyPartRecord (the column isn't extended with NotNull(), and the property in the record has the StringLengthMax attribute), but the problem still persists.
However, I am able to create a page or any content type with text longer, than 4000 characters on the admin interface.
Our site uses SQL Server Compact.

There are several topics on the internet regarding the same issue, but with different numbers (eg. 256), some of them suspects NHibernate or the database server, but what's common in them is that they didn't offer any viable solution for me.

Jun 10, 2012 at 7:01 PM

Do you have repro steps?

Jun 10, 2012 at 8:14 PM
Edited Jun 10, 2012 at 8:19 PM

I'll link source code, the problem occured while developing a module (OrchardHUN.ModuleProfiles):

The "Definition" property contains the JSON-serialized string that is sometimes longer, that 4000 characters. The action currently contains a condition check, which prevents the user from saving a profile that would cause the exception (eg. by clicking "Save Configuration"), but without it, an InvalidOperationException is thrown:
@p1 : String truncation: max=4000, len=6542, value='...'.

Jun 10, 2012 at 8:42 PM

Did you try without NotNull? Are you sure it's that version of your migration that ran?

Jun 10, 2012 at 8:51 PM

Yep, I tried that (I deleted the table and the module's DataMigrationRecord row), still throws the exception.

Jun 10, 2012 at 9:14 PM

Are you also seeing the problem with the body part? What database are you using?

Jun 11, 2012 at 9:15 AM

Nope, BodyPart saves without error with any length of text. The database is SQL CE.

Jun 11, 2012 at 3:39 PM

You need to add [StringMaxLength] to the property in the Record class.

Jun 11, 2012 at 3:56 PM
Edited Jun 11, 2012 at 4:03 PM

I tried adding [StringLengthMax] to the property before, but it didn't help, with or without NotNull() either.
Anyway, the Unlimited() extension makes the column's type NText, and the attribute doesn't seem to change it (as far as I can tell from what WebMatrix shows).

Jun 11, 2012 at 8:32 PM

Well, there's nothing magical about body really, so I'd keep looking for differences in your code.

Jun 13, 2012 at 6:44 PM

Well, in the end, [StringLengthMax] brought broke the ice (for the 3rd try) and it works now. But it took 3 server restarts to make it work, before that it threw the exception as before. Is it possible that it takes some time to NHibernate to be aware of the changes?

Jun 13, 2012 at 8:50 PM

Definitely, because the mapping needs to be refreshed. It's not a db issue, but what NH thinks it is. So restarting the nodes will regenerate the mapping.

Jun 14, 2012 at 6:53 AM

Well, I tried several combinations of the solutions suggested (including deleting mappings.bin), it may be that I missed the right one before. Thanks for your help!

Mar 7, 2013 at 9:46 AM
Just for reference: the correct attribute name is [StringLengthMax] just as used in BodyPartRecord.cs.