NHibernate的PostgreSQL的日期时间,以时间转换 [英] NHibernate Postgresql DateTime to Time Conversion
问题描述
我用流利的NHibernate的读取数据时配置我的映射一切正常,但试图插入或当有一个PostgreSQL类型的时间我得到错误信息更新记录
I'm using Fluent NHibernate to configure my mappings everything works when reading data but when trying to insert or update records that have a Postgresql type of Time I get the error message
错误:42804:列\run_time \的类型是时间和时区,但EX pression是timestamp类型没有时区
它看起来像NHibernate的可能会搞不清楚哪个的DbType到日期时间转换为可以从 PostgreSQL的看和C#的数据类型的,有几个DbTypes的日期时间地图。
It looks like NHibernate might be confused on which DbType to convert the DateTime to as you can see from PostgreSQL and C# Datatypes that there are several DbTypes that DateTime map to.
我也试过指定自定义类型从IUserType但对NullSafeSet覆盖此专栏中,我得到一个NullReferenceError
I also tried specifying a custom type for this column from IUserType but on the NullSafeSet override I get a NullReferenceError
public override void NullSafeSet(IDbCommand cmd, object value, int index)
{
var obj = (DateTime)value;
NHibernate.NHibernateUtil.Time.NullSafeSet(cmd, obj, index);
}
有一些类型的暗示我可以提供要么告诉NHibernate的于日期转换为一个PostgreSQL时间类型?或者,我可以做到这一点的IUserType,我只是做错了什么?
Is there some type of hint I can provide to either tell NHibernate to convert the DateTime to a Postgresql "time" type? Or can I achieve this by the IUserType and I'm just doing something wrong?
推荐答案
想通了!
显然转换片我链接以要么是错误或过时。事实证明,一个System.TimeSpan对象是我们所需要的Npgsql做一个适当的转换,以一个PostgreSQL时间的对象。这似乎很奇怪,我说,他们将试图将其转换重新presents之间的两个时间的差别成我们所认为的HH东西:MM:SS但是这事情是这样的。
Apparently the conversion sheet I linked to is either wrong or out of date. It turns out that a System.TimeSpan object is what's needed for Npgsql to do a proper conversion to a Postgresql "time" object. It seems odd to me that they would try to convert something that represents a difference between two time's into what we think of as HH:mm:ss but that's the way it is.
而不是改变从System.DateTime,它以System.TimeSpan我,而不是创建一个自定义IUserType我运行时属性的类型,并重写NullSafeSet看起来像
Rather than change the type of my RunTime property from System.DateTime to System.TimeSpan I've instead created a custom IUserType and have overriden NullSafeSet to look like
public override void NullSafeSet(IDbCommand cmd, object value, int index)
{
var obj = (DateTime)value;
((IDbDataParameter) cmd.Parameters[index]).Value = new TimeSpan(0, obj.Hour, obj.Minute, obj.Second);
}
这篇关于NHibernate的PostgreSQL的日期时间,以时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!