ServiceStack.OrmLite: StringLengthAttribute.MaxText produces “LONGTEXT” – how can I set to “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:

enter image description here

But it becomes LONGTEXT:

enter image description here

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.

Question is: how can I force it to become “TEXT” (or any other text type in mysql)?

I could perhaps modify the MaxColumnDefinition and set it to TEXT but then I will never get LONGTEXT I assume.

enter image description here

Update

Setting the MaxColumnDefinition to TEXT didn’t change a thing.

Can I decorate the class with an attribute, specifying the type somehow? Or is that too sql-version-specific?

Or perhaps I should override the GetColumnDefinition and implement my own logic…

Answer

(Please note that this solution produces other problems that I will ask in a separate SO post)

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";
        }
    }
}

I also suggest setting the default StringLength to something smaller than the default 8000:

StringConverter converter = OrmLiteConfig.DialectProvider.GetStringConverter();
converter.StringLength = 255;

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

Leave a Reply

Your email address will not be published. Required fields are marked *