servicestack ormlite を使用する場合、テーブルに列を作成して varchar(max) として指定するにはどうすればよいですか?
現在、テーブルの作成後にSQLを実行して、必要なものを取得しています。私は StringLength 属性を見てきましたが、それを行うより良い方法があるかどうか疑問に思っていましたか?
何かのようなもの
StringLength(Sql.VarcharMax)
ありがとう
servicestack ormlite を使用する場合、テーブルに列を作成して varchar(max) として指定するにはどうすればよいですか?
現在、テーブルの作成後にSQLを実行して、必要なものを取得しています。私は StringLength 属性を見てきましたが、それを行うより良い方法があるかどうか疑問に思っていましたか?
何かのようなもの
StringLength(Sql.VarcharMax)
ありがとう
次のようにして問題を回避しました
if (!db.TableExists(typeof(Entity).Name))
{
db.CreateTableIfNotExists<Entity>();
db.ExecuteSql("ALTER TABLE Entity ALTER COLUMN Column VARCHAR(MAX)");
}
ドメインモデルを装飾するSystem.ComponentModel.DataAnnotations.StringLengthAttribute
そのような
[StringLengthAttribute(8001)]
public string Markdown { get;set; }
また
[StringLength(Int32.MaxValue)]
public string Markdown { get;set; }
8000 を超える長さを使用して、宣言が必要な SQL Server varchar
/nvarchar
列タイプの最大長を超えています。MAX
MAX
宣言を理解するカスタム方言プロバイダーを使用します。
public class MaxSqlProvider : SqlServerOrmLiteDialectProvider
{
public new static readonly MaxSqlProvider Instance = new MaxSqlProvider ();
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);
var max = string.Format(StringLengthColumnDefinitionFormat, "MAX");
fieldDefinition = fieldDefinition.Replace(orig, max);
}
return fieldDefinition;
}
}
データベース ファクトリを構築するときにプロバイダーを使用する
var dbFactory = new OrmLiteConnectionFactory(conStr, MaxSqlProvider.Instance);
この図は特に SqlServer を対象としていますが、目的のプロバイダーから継承した後に小さな変更を加えた他のデータ プロバイダーにも関連することに注意してください。
ServiceStack は、フィールドの作成タイプをさらにカスタマイズする機能でこれに対処しているようです。https ://github.com/ServiceStack/ServiceStack.OrmLite#custom-field-declarations を参照してください。
またはそのリンクで詳しく説明されているように:
[CustomField] 属性は、生成された Create table DDL ステートメントでカスタム フィールド宣言を指定するために使用できます。
public class PocoTable
{
public int Id { get; set; }
[CustomField("CHAR(20)")]
public string CharColumn { get; set; }
[CustomField("DECIMAL(18,4)")]
public decimal? DecimalColumn { get; set; }
}
どこ
db.CreateTable<PocoTable>();
次の SQL を生成して実行します。
CREATE TABLE "PocoTable"
(
"Id" INTEGER PRIMARY KEY,
"CharColumn" CHAR(20) NULL,
"DecimalColumn" DECIMAL(18,4) NULL
);
ただし、私のコメント o 以前の回答の 1 つによると、これはおそらく db 固有のものであると推測しています。申し訳ありませんが、テストする複数の db サーバーに簡単にアクセスすることはできません。
各型の列定義は、デフォルトのSqlServerStringConvertersが属性を持つプロパティに使用するOrmLite の型コンバーターを使用して制御できます。VARCHAR(MAX)
[StringLength(StringLengthAttribute.MaxText)]
public class PocoTable
{
public int Id { get; set; }
[StringLength(StringLengthAttribute.MaxText)]
public string MaxText { get; set; }
[StringLength(256)]
public string SmallText { get; set; }
}
Using StringLengthAttribute.MaxText
(またはそのエイリアスint.MaxValue
) は、各 RDBMS に大きな文字列を格納するための最も適切なデータ型を自動的に使用します。