3

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?

AnimaSola
  • 7,146
  • 14
  • 43
  • 62

4 Answers4

6

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; }
}
AnimaSola
  • 7,146
  • 14
  • 43
  • 62
2

This error is coming from SQL Server. If you wanted the strings to be longer you'll need to modify the SQL schema to use something like VARCHAR(MAX).

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • I'm currently using SQL compact for local/dev machine. But the stage and prod deployment are on SQL Azure. Does that make any difference? (i.e. does sql compact have char limitations even with string unlimited?) – AnimaSola Aug 19 '13 at 13:28
  • @AnimaSola, yes, 4000 characters is the absolute maximum for SQL CE, http://stackoverflow.com/questions/6736881/sql-ce-max-length. – Mike Perrenoud Aug 19 '13 at 13:31
  • 1
    It's also required to add the StringLengthMax attribute to the model property. This tells NHibernate not to truncate. – Brandon Joyce Aug 19 '13 at 19:57
1

This is a database problem.

For SQL Server 2008 and later, use VARCHAR(MAX).

For SQL Server 2005 and earlier, you need to explicitly state the varchar size, like VARCHAR(5000), with VARCHAR(8000) being the limit.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • Likewise with The Solution, let's say I don't have control of SQL. However, I'm currently using SQL compact for local/dev machine. But the stage and prod deployment are on SQL Azure. Does that make any difference? (i.e. does sql compact have char limitations even with string unlimited?) – AnimaSola Aug 19 '13 at 13:31
  • For SQL Server Compact Edition 4.0, the max character size is 4,000. Read [Data Types](http://technet.microsoft.com/en-us/library/ms172424.aspx) for SQL Server CE 4.0. Note: `ntext` would be an option, except it is no longer supported for string functions. – Karl Anderson Aug 19 '13 at 13:38
  • Have you considered [SQL Server 2012 Express](http://www.microsoft.com/en-us/download/details.aspx?id=29062) for your local development environment? That would more closely mirror the capabilities of SQL Azure (your PRODUCTION environment)? – Karl Anderson Aug 19 '13 at 13:39
  • Yeah I have SSMS. Just using compact for quick deployments with Orchard. Just wanted to confirm this was a SQL issue and not with the app. Thanks! – AnimaSola Aug 19 '13 at 13:40
0

Another way to solve this problem is configurating the Map like this:

Map(l => l.Description ).CustomType("StringClob").CustomSqlType("varchar(max)");
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42