简体   繁体   English

ServiceStack.OrmLite中的ntext

[英]ntext in ServiceStack.OrmLite

how can i have nText datatype in ServiceStack.OrmLite Code first ? 我怎样才能在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; } 

}

if i use string datatype , ormlite generate nVarchar(8000) in database 如果我使用字符串数据类型,ormlite在数据库中生成nVarchar(8000)

i need more than 8000 character for data 我需要超过8000个字符的数据

You need to convert your Body type to byte[] from string for ServiceStack.OrmLite to use the varchar(max) column type. 您需要从ServiceStack.OrmLite的stringBody类型转换为byte[]以使用varchar(max)列类型。 Something like the below: 像下面这样的东西:

public byte[] Body { get; set; }

The reason for this lies within the ServiceStack.OrmLite code. 其原因在于ServiceStack.OrmLite代码。

In the file ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs , under the method InitColumnTypeMap() is: ServiceStack.OrmLite / OrmLiteDialectProviderBase.cs文件中, InitColumnTypeMap()方法是:

DbTypeMap.Set<byte[]>(DbType.Binary, BlobColumnDefinition);

In the file ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs , under the method SqlServerOrmLiteDialectProvider() is: ServiceStack.OrmLite.SqlServer / SqlServerOrmLiteDialectProvider.cs文件中, SqlServerOrmLiteDialectProvider()方法是:

base.BlobColumnDefinition = "VARBINARY(MAX)";

From this code, you can see that an internal mapping is taking place from the C# type to the internal ServiceStack.OrmLite type and then back out to the SqlServer type. 从此代码中,您可以看到内部映射从C#类型发生到内部ServiceStack.OrmLite类型,然后返回到SqlServer类型。

This question explains how to convert back and forth between strings and byte arrays, How do I get a consistent byte representation of strings in C# without manually specifying an encoding? 这个问题解释了如何在字符串和字节数组之间来回转换, 如何在不手动指定编码的情况下在C#中获得字符串的一致字节表示? .

Assuming you really want NTEXT . 假设你真的想要NTEXT If you want nvarchar(max) or varchar(max) see https://stackoverflow.com/a/25729568/37055 如果你想要nvarchar(max)varchar(max)请参阅https://stackoverflow.com/a/25729568/37055

Decorate your domain model with System.ComponentModel.DataAnnotations.StringLengthAttribute 使用System.ComponentModel.DataAnnotations.StringLengthAttribute装饰您的域模型

such as

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

or 要么

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

using any length greater than 8000 to exceed to maximum length of Sql Server varchar / nvarchar column types. 使用任何大于8000的长度超过Sql Server varchar / nvarchar列类型的最大长度。

Use a custom dialect provider the understands the NTEXT declaration. 使用自定义方言提供程序了解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;
  }
}

Use the provider when you construct the database factory 在构造数据库工厂时使用提供程序

var dbFactory = new OrmLiteConnectionFactory(conStr, NTextSqlProvider.Instance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM