OrmLite [StringLength()] and Performance

Is it recommended to specify the [StringLength()] on each string property when using OrmLite?

I’m using MySql and from what I’ve been reading the overall consensus seems to be that smaller can improve performance.

It’s up to each RDBMS what their effect of handling VARCHAR’s with varying lengths is. In MySql any VARCHAR up to VARCHAR(255) is stored the same way which is the default length for string in OrmLite so a custom [StringLength] wouldn’t yield any improvement.

You would use it when you want to hold larger strings, e.g. using MaxText will create the text column as LONGTEXT:

public class CacheEntry
{
    public string Id { get; set; }
    [StringLength(StringLengthAttribute.MaxText)]
    public string Data { get; set; }
}
1 Like