如何将ServiceStack POCO的默认数据类型从文本更改为json?

时间:2014-08-21 22:19:57

标签: json postgresql servicestack

这是PostgreSQL特有的,但我仍然想知道它是否可能。

鉴于此示例

public class Borrower
{
    public Borrower()
    {
        PhoneNumbers = new Dictionary<PhoneType, string>();
        Addresses = new Dictionary<AddressType, BorrowerAddress>();
    }

    [AutoIncrement] // Creates Auto primary key
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    [Index(Unique = true)] // Creates Unique Index
    public string Email { get; set; }

    public Dictionary<PhoneType, string> PhoneNumbers { get; set; }  //Blobbed
    public Dictionary<AddressType, BorrowerAddress> Addresses { get; set; }  //Blobbed
    public DateTime CreatedAt { get; set; }
}



var dbFactory = new OrmLiteConnectionFactory(
    ConfigUtils.GetConnectionString("Database:MyDatabase"), PostgreSqlDialect.Provider);

using (var db = dbFactory.Open())
{
    db.DropAndCreateTable<BorrowerAddress>();
    db.DropAndCreateTable<Borrower>();


    var customer = new Borrower
    {
        FirstName = "Orm",
        LastName = "Lite",
        Email = "stephenpatten@foo.com",
        PhoneNumbers =
          {
              { PhoneType.Home, "555-1234" },
              { PhoneType.Work, "1-800-1234" },
              { PhoneType.Mobile, "818-123-4567" },
          },
        Addresses =
          {
              { AddressType.Work, new BorrowerAddress { 
                Line1 = "1 Street", Country = "US", State = "NY", City = "New York", ZipCode = "10101" } 
              },
          },
        CreatedAt = DateTime.UtcNow,
    };

    var customerId = db.Insert(customer, selectIdentity: true); 
    customer = db.Single<Borrower>(new { customer.Email });
    Debug.Assert(customer.Id == customerId, "Ids are the same");
}

执行此操作后,这里是借款人表的架构

Borrower Table

和刚插入的数据为json。

enter image description here

所以我的问题是,“我可以通过属性或其他一些机制更改POCO上的数据类型以支持json数据类型吗?”

谢谢你, 斯蒂芬

1 个答案:

答案 0 :(得分:4)

您可以使用[CustomField]属性更改数据类型,例如:

public class Borrower
{
    ...
    [CustomField("json")]
    public Dictionary<PhoneType, string> PhoneNumbers { get; set; } 
}

虽然可以让您创建一个包含json字段的表格,但您仍然无法使用OrmLite插入记录,因为它需要一些advanced customizations with the Ngpqsl PostgreSQL Driver。我们将考虑在未来添加与Npgsql更深层次的集成以支持这一点。