如何在 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 个字符的数据
您需要转换您的东西,如下所示:
其原因在于代码中。
从这段代码中,你可以看到
假设您真的想要
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);