2013-10-12 66 views
1

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

使用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; } 
    } 
} 

回答

0

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

+0

了解重新varchar数据类型,问题的目的是为了设置大小上限的最佳方法,它缺省为varchar(255)为没有注释的blobbed类型。手动上浆似乎是要走的路,但想看看是否有更好的或替代的选择。 – Steve

+0

添加上面的单元测试示例来说明 – Steve