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

问题描述 投票:3回答:4

我正在使用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]属性,我需要导入哪些命名空间或类?

c# asp.net-mvc nhibernate orchardcms orchardcms-1.6
4个回答
6
投票

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

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

  1. 对于DB,您只需将列定义为无限制,就像我最初完成的那样。确保数据库中的列为nvarchar(max)或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才能使用该属性。

见下文:

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

2
投票

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


1
投票

这是一个数据库问题。

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

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


0
投票

解决此问题的另一种方法是将Map配置为:

Map(l => l.Description ).CustomType("StringClob").CustomSqlType("varchar(max)");
© www.soinside.com 2019 - 2024. All rights reserved.