NHibernate.HibernateException: The length of the string value exceeds the length configured in the mapping/parameter.

I have found NHibernate.HibernateException: The length of the string value exceeds the length configured in the mapping/parameter. in your Orchard CMS log.

SchemaBuilder.CreateTable(typeof(ServiceRequestCacheRecord).Name, table => table
                .Column<int>("Id", x => x.PrimaryKey().Identity())
                .Column<int>("ServiceId")
                .Column<string>("RequestKey", x => x.WithLength(50))
                .Column<string>("Data", x=> x.Unlimited())   
                .Column("LastUpdate", DbType.DateTime)
            );

The problem was in "Data" column, it stores long json. It was created by Orchard in MS SQL Server 2008 database as nvarchar(MAX), but NHibernate does not allow to insert 20 000 length string to it.

The solution is to put [StringLengthMax] attribute to Data field.

public class ServiceRequestCacheRecord
{
        public virtual int Id { get; set; }
        public virtual int ServiceId { get; set; }
        public virtual string RequestKey { get; set; }
        [StringLengthMax]
        public virtual string Data { get; set; }
        public virtual DateTime? LastUpdate { get; set; }
}