ServiceStack.OrmLite:StringLengthAttribute.MaxText 生成“LONGTEXT";- 如何设置为“文本"? [英] ServiceStack.OrmLite: StringLengthAttribute.MaxText produces "LONGTEXT" - how can I set to "TEXT"?
问题描述
使用 ServiceStack 的 OrmLite 并使用以下属性装饰属性,创建一个 LONGTEXT 类型的列,而不是文档中所述的 o TEXT:
Using ServiceStack's OrmLite and decorating a Property with the following attribute, creates a column of type LONGTEXT, instead o TEXT as noted in the docs:
但它变成了 LONGTEXT:
But it becomes LONGTEXT:
我尝试将 StringLenghtAttribute
设置为其他内容,65535
、70000
、Int32.MaxValue
等,但是它要么将其解释为 VARCHAR 并给我 Column length too big
或 Row size too large
,或者它变成了 LONGTEXT.
I tried setting the StringLenghtAttribute
to something else, 65535
, 70000
, Int32.MaxValue
etc, but it either interpretet it as a VARCHAR and gave me the Column length too big
or Row size too large
, or it became a LONGTEXT.
问题是:我怎样才能强迫它变成TEXT"?(或 mysql 中的任何其他文本类型)?
Question is: how can I force it to become "TEXT" (or any other text type in mysql)?
我也许可以修改 MaxColumnDefinition
并将其设置为 TEXT
但我假设我永远不会得到 LONGTEXT.
I could perhaps modify the MaxColumnDefinition
and set it to TEXT
but then I will never get LONGTEXT I assume.
将 MaxColumnDefinition
设置为 TEXT 并没有改变任何事情.
Setting the MaxColumnDefinition
to TEXT didn't change a thing.
我可以用属性装饰类,以某种方式指定类型吗?还是太特定于 sql 版本?
Can I decorate the class with an attribute, specifying the type somehow? Or is that too sql-version-specific?
或者也许我应该覆盖 GetColumnDefinition
并实现我自己的逻辑......
Or perhaps I should override the GetColumnDefinition
and implement my own logic...
推荐答案
(请注意,此解决方案会产生其他问题,我将在单独的 SO 帖子中提出)
经过更多研究,解决了创建我自己的 StringConverter 的问题,如下所示:
After some more research, it was solved creating my own StringConverter, as can be seen below:
public class MyStringConverter : StringConverter
{
public override string GetColumnDefinition(int? stringLength)
{
if (stringLength.GetValueOrDefault() == StringLengthAttribute.MaxText)
return MaxColumnDefinition;
if (stringLength.GetValueOrDefault(StringLength) <= 255)
{
return UseUnicode
? $"NVARCHAR({stringLength.GetValueOrDefault(StringLength)})"
: $"VARCHAR({stringLength.GetValueOrDefault(StringLength)})";
}
else if (stringLength.GetValueOrDefault(StringLength) <= 65535)
{
return $"TEXT";
}
else
{
return "LONGTEXT";
}
}
}
我还建议将默认 StringLength 设置为小于默认 8000 的值:
I also suggest setting the default StringLength to something smaller than the default 8000:
StringConverter converter = OrmLiteConfig.DialectProvider.GetStringConverter();
converter.StringLength = 255;
这样,默认的 string
将是 varchar(255)
.如果你想让一个特定的字符串属性变成别的东西,那么根据 MyStringConverter
设置属性:
This way, the default string
will be varchar(255)
. If you want to make a specific string property something else, then set the attribute according to the MyStringConverter
:
type = typeof(tWorks.Core.CoreCommons.ContactModuleProtocols.SMS.ModuleSettingsSMS);
type.GetProperty(nameof(MyClass.Prop1)).AddAttributes(new StringLengthAttribute(255)); // ==> varchar(255)
type.GetProperty(nameof(MyClass.Prop2)).AddAttributes(new StringLengthAttribute(500)); // ==> TEXT
type.GetProperty(nameof(MyClass.Prop3)).AddAttributes(new StringLengthAttribute(70000)); // ==> LONGTEXT
这篇关于ServiceStack.OrmLite:StringLengthAttribute.MaxText 生成“LONGTEXT";- 如何设置为“文本"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!