Search code examples
c#asp.net-mvcnhibernateorchardcmsorchardcms-1.6

String Unlimited still limited to 4000 characters?


I'm using Orchard 1.6 and I'm creating parts for my module. The piece in my migration file that creates the specific table is:

    // Creating table SessionInformationRecord
    SchemaBuilder.CreateTable("SessionInformationRecord", table => table
        .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
        .Column("TrackInformationRecord_Id", DbType.Int32)
        .Column("Title", DbType.String, col => col.Unlimited())
        .Column("Description", DbType.String, col => col.Unlimited())
        .Column("StartDate", DbType.DateTime)
        .Column("EndDate", DbType.DateTime)
        .Column("HasEvaluation", DbType.Boolean)
        .Column("IsDeleted", DbType.Boolean)
    );

The Title and Description are supposed to be unlimited Strings. However, When I enter content for those fields that are beyond 4000 characters, I get this error:

{"@p1 : String truncation: max=4000, len=21588, value=''."}

Any other way to get around this? Or is 4000 characters the max for a String?

UPDATE:

Aside from the DB side, I read that you also have to handle it on the NHibernate side to make sure it doesn't truncate the string. People have told me to add the attribute:

[StringLengthMax]

However, my model only recognizes the [StringLength] attribute. What namespace or class do I need to import in order to use the [StringLengthMax] attribute?


Solution

  • While the answers at the bottom are correct, they are not complete.

    To avoid the 4000 character limit, it must be handled on both the DB and NHibernate.

    1. For the DB, you just have to define the column to be unlimited as I have initially done. Ensure that the column is nvarchar(max) or varchar(max) in the DB.

      // Creating table KeynoteInformationRecord
      SchemaBuilder.CreateTable("KeynoteInformationRecord", table => table
          .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
          .Column("KeynotePartId", DbType.Int32)
          .Column("Title", DbType.String, column => column.Unlimited())
          .Column("Description", DbType.String, column => column.Unlimited())
          .Column("StartDate", DbType.DateTime)
          .Column("EndDate", DbType.DateTime)
          .Column("HasEvaluation", DbType.Boolean)
          .Column("IsDeleted", DbType.Boolean)
      );
      
    2. On the Nhibernate end, to ensure the string is not truncated, you have to add the [StringLengthMax] attribute onto your string properties within your model classes. In Orchard, you have to include Orchard.Data.Conventions to use the property.

    See below:

    public class KeynoteInformationRecord
    {
        public virtual int Id { get; set; }
        public virtual int KeynotePartId { get; set; }
        [StringLengthMax]
        public virtual string Title { get; set; }
        [StringLengthMax]
        public virtual string Description { get; set; }
        public virtual DateTime StartDate { get; set; }
        public virtual DateTime EndDate { get; set; }
        public virtual bool HasEvaluation { get; set; }
        public virtual bool IsDeleted { get; set; }
    }