【发布时间】:2016-03-16 11:59:03
【问题描述】:
我有一个复杂的对象,我使用 Ormlite 将它保存到 postgres 中的 JsonB 字段中。 其中一个属性是 DateTime,设置为 DateTime.Max。
从 Postgres 中检索对象将 DateTime 属性值设置为 DateTime.Min 值
01/01/0001 00:00:00
不确定这是 Ormlite 还是 json 序列化程序的错误。
要复制的代码 sn-p
class Program
{
static void Main(string[] args)
{
var item = new LicenseCheckTemp();
item.Body = new CheckHistory();
item.Body.List.Add(new ItemHistory() {AddedOn = DateTime.MaxValue, Note = "Test"});
var factory = GetFactory(ConfigurationManager.AppSettings["PostgresConnectionString"]);
using (var db = factory.OpenDbConnection())
{
db.CreateTableIfNotExists<LicenseCheckTemp>();
db.Save(item);
}
using (var db = factory.OpenDbConnection())
{
var items = db.Select<LicenseCheckTemp>();
foreach (var licenseCheck in items.OrderBy(x=>x.Id))
{
if (licenseCheck.Body != null && licenseCheck.Body.List.Any())
{
foreach (var itemHistory in licenseCheck.Body.List)
{
Console.WriteLine($"{itemHistory.AddedOn} : Note {itemHistory.Note}");
}
}
}
}
Console.ReadKey();
}
public static IDbConnectionFactory GetFactory(string connection)
{
var factory = new OrmLiteConnectionFactory(connection,
PostgreSqlDialect.Provider);
factory.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();
return factory;
}
}
public class LicenseCheckTemp
{
[AutoIncrement]
public int Id { get; set; }
[CustomField("json")]
public CheckHistory Body { get; set; }
}
public class CheckHistory
{
public List<ItemHistory> List { get; set; } = new List<ItemHistory>();
}
public class ItemHistory
{
public string Note { get; set; }
public DateTime AddedOn { get; set; }
}
【问题讨论】:
-
请发布显示问题的确切源代码。
-
嗨,mythz,添加了代码 sn-p
标签: postgresql servicestack ormlite-servicestack