MySQL中的ServiceStack ORMLite blobbed列

时间:2013-10-12 02:03:16

标签: ormlite-servicestack

我正在使用OrmLite for MySql(来自nuget)并且有一些我坚持的对象会导致内容被序列化和blobbed。我发现这些字段的模式默认为varchar(255),仅适用于小blob。即使相对较小的列表也太大而不能容纳255个字符。

使用OrmLite时,确保blob表大小正确的最佳方法是什么?

示例:

public class Foo : IHasId<long>
{
  [AutoIncrement]
  public long Id { get; set; }

  public Dictionary<string, string> TestSize { get; set; }
}

我现在采用的方法是使用[StringLength(6000)]为每个blobbed字段添加注释。虽然这有效,但我不确定是否有更好的方法来确保足够的空间。

下面是一个完整的单元测试,用于说明调整问题:

using NUnit.Framework;
using ServiceStack.DataAnnotations;
using ServiceStack.DesignPatterns.Model;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.MySql;
using System;
using System.Collections.Generic;
using System.Configuration;

namespace OrmLiteTestNamespace
{
    [TestFixture]
    public class BlobItemTest
    {
        [Test]
        public void TableFieldSizeTest()
        {
            var dbFactory = new OrmLiteConnectionFactory(
                  ConfigurationManager.AppSettings["mysqlTestConn"],
                  MySqlDialectProvider.Instance);
            using (var db = dbFactory.OpenDbConnection())
            {
                db.CreateTableIfNotExists<Foo>();
                var foo1 = new Foo()
                    {
                        TestSize = new Dictionary<string, string>()
                    };

                // fill the dictionary with 300 things
                for (var i = 0; i < 300; i++)
                {
                    foo1.TestSize.Add(i.ToString(), Guid.NewGuid().ToString());
                }
                // throws MySql exception "Data too long for column 'TestSize' at row 1"
                db.Insert(foo1);

            }
        }

    }
    public class Foo : IHasId<long>
    {
        [AutoIncrement]
        public long Id { get; set; }

        public Dictionary<string, string> TestSize { get; set; }
    } 
}

1 个答案:

答案 0 :(得分:0)

Varchar数据类型是变量大小的数据类型,其空间仅由字段内容决定,而不是列定义的大小。例如。在MySQL中,它只占用size of the contents + 2 bytes for the length(最长65535)。