【问题标题】:String Unlimited still limited to 4000 characters?String Unlimited 仍限制为 4000 个字符?
【发布时间】:2013-08-19 13:23:21
【问题描述】:

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

【问题讨论】:

  • 问题是数据库的 max(xxxx) 设置为 4000。正如字符串解释它。

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


【解决方案1】:

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

为避免 4000 个字符的限制,必须同时在 DB 和 NHibernate 上处理。

  1. 对于数据库,您只需像我一样将列定义为无限制 已经初步完成。确保该列是 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) 的内容。

    【讨论】:

    • 我目前在本地/开发机器上使用 SQL compact。但是 stage 和 prod 部署在 SQL Azure 上。这有什么区别吗? (即 sql compact 是否有字符限制,即使字符串无限制?)
    • @AnimaSola,是的,4000 个字符是 SQL CE 的绝对最大值,stackoverflow.com/questions/6736881/sql-ce-max-length
    • 还需要在模型属性中添加 StringLengthMax 属性。这告诉 NHibernate 不要截断。
    【解决方案3】:

    这是一个数据库问题。

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

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

    【讨论】:

    • 与解决方案类似,假设我无法控制 SQL。但是,我目前正在为本地/开发机器使用 SQL compact。但是 stage 和 prod 部署在 SQL Azure 上。这有什么区别吗? (即 sql compact 是否有字符限制,即使字符串无限制?)
    • 对于 SQL Server Compact Edition 4.0,最大字符大小为 4,000。阅读Data Types 了解 SQL Server CE 4.0。注意:ntext 是一个选项,但字符串函数不再支持它。
    • 您是否考虑过SQL Server 2012 Express 用于您的本地开发环境?这将更接近地反映 SQL Azure(您的生产环境)的功能?
    • 是的,我有 SSMS。只需使用紧凑型与 Orchard 进行快速部署。只是想确认这是一个 SQL 问题,而不是应用程序。谢谢!
    【解决方案4】:

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

    Map(l => l.Description ).CustomType("StringClob").CustomSqlType("varchar(max)");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 2010-10-23
      • 2015-04-02
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多