ServiceStack.OrmLite 中的ntext

问题描述 投票:0回答:2

如何在 ServiceStack.OrmLite 代码中先使用 nText 数据类型?

public class Email
{
    [AutoIncrement]
    public long ID { get; set; }


    public DateTime Date { get; set; }

    public string From { get; set; }

    public string Subject { get; set; } 

    nText =>
    public string Body { get; set; } 

}

如果我使用字符串数据类型,ormlite 在数据库中生成 nVarchar(8000)

我需要超过 8000 个字符的数据

c# sql-server code-first servicestack ormlite-servicestack
2个回答
4
投票

您需要转换您的东西,如下所示:

其原因在于代码中。

从这段代码中,你可以看到


2
投票

假设您真的想要

NTEXT
。如果您想要
nvarchar(max)
varchar(max)
,请参阅 https://stackoverflow.com/a/25729568/37055

System.ComponentModel.DataAnnotations.StringLengthAttribute

装饰你的领域模型

比如

[StringLengthAttribute(8001)]
public string Markdown { get;set; }

[StringLength(Int32.MaxValue)]
public string Markdown { get;set; }

使用任何大于 8000 的长度来超出 Sql Server

varchar
/
nvarchar
列类型的最大长度。

使用自定义方言提供程序来理解

NTEXT
声明。

public class NTextSqlProvider : SqlServerOrmLiteDialectProvider
{
  public new static readonly NTextSqlProvider Instance = new NTextSqlProvider();

  public override string GetColumnDefinition(string fieldName, Type fieldType, 
            bool isPrimaryKey, bool autoIncrement, bool isNullable, 
            int? fieldLength, int? scale, string defaultValue)
  {
     var fieldDefinition = base.GetColumnDefinition(fieldName, fieldType,
                                    isPrimaryKey, autoIncrement, isNullable, 
                                    fieldLength, scale, defaultValue);

     if (fieldType == typeof (string) && fieldLength > 8000)
     {
       var orig = string.Format(StringLengthColumnDefinitionFormat, fieldLength);

       fieldDefinition = fieldDefinition.Replace(orig, "NTEXT");
     }

     return fieldDefinition;
  }
}

构建数据库工厂时使用提供程序

var dbFactory = new OrmLiteConnectionFactory(conStr, NTextSqlProvider.Instance);
© www.soinside.com 2019 - 2024. All rights reserved.