String Unlimited仍然限制为4000个字符?

时间:2013-08-19 13:23:22

标签: c# asp.net-mvc nhibernate orchardcms orchardcms-1.6

我正在使用Orchard 1.6,我正在为我的模块创建零件。我的迁移文件中创建特定表的部分是:

    // 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)
    );

标题和描述应该是无限的字符串。但是,当我输入超过4000个字符的字段的内容时,我收到此错误:

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

还有其他方法来解决这个问题吗?或者字符串最大为4000个字符?

更新

除了数据库方面,我还读到你必须在NHibernate端处理它,以确保它不会截断字符串。人们告诉我要添加属性:

[StringLengthMax]

但是,我的模型只识别[StringLength]属性。为了使用[StringLengthMax]属性,我需要导入哪些命名空间或类?

4 个答案:

答案 0 :(得分:6)

虽然底部的答案是正确的,但它们并不完整。

要避免4000字符限制,必须在DB和NHibernate上处理它。

  1. 对于数据库,您只需要将列定义为无限制 最初做了。确保列为nvarchar(max)或 DB中的varchar(max)。

    // 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. 在Nhibernate端,为了确保字符串不被截断,您必须将[StringLengthMax]属性添加到模型类中的字符串属性中。在Orchard中,您必须包含Orchard.Data.Conventions才能使用该属性。

  3. 见下文:

    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; }
    }
    

答案 1 :(得分:2)

此错误来自SQL Server。如果您希望字符串更长,则需要修改SQL模式以使用VARCHAR(MAX)之类的内容。

答案 2 :(得分:1)

这是一个数据库问题。

对于SQL Server 2008及更高版本,请使用VARCHAR(MAX)

对于SQL Server 2005及更早版本,您需要显式声明varchar大小,例如VARCHAR(5000),其中VARCHAR(8000)为限制。

答案 3 :(得分:0)

解决此问题的另一种方法是像这样配置地图:

Map(l => l.Description ).CustomType("StringClob").CustomSqlType("varchar(max)");
相关问题