レコードのUTC作成タイムスタンプをデフォルトにするIUserTypeクラスを作成しました。正しく実装したと思いましたが、データベースには何も保存されません。誰かが私が間違っていることを教えてもらえますか?クラスは次のようになります。
public class UTCTimeStamp : IUserType
{
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object DeepCopy(object value)
{
var orig = value as Nullable<DateTime>;
if (orig == null) return null;
var newVal = new DateTime(
((DateTime)orig).Ticks);
return newVal;
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
public bool Equals(object x, object y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.Equals(y);
}
public int GetHashCode(object x)
{
if (x == null)
{
throw new ArgumentNullException("Argument cannot be null.");
}
return x.GetHashCode();
}
public bool IsMutable
{
get { return false; }
}
public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
{
var timestamp = NHibernate.NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
if (timestamp != null && timestamp is DateTime)
{
return (DateTime)timestamp;
}
return null;
}
public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
if (value == null || !(value is Nullable<DateTime>))
{
NHibernate.NHibernateUtil.DateTime.NullSafeSet(cmd, DateTime.UtcNow, index);
return;
}
NHibernate.NHibernateUtil.DateTime.NullSafeSet(
cmd, value, index);
}
public object Replace(object original, object target, object owner)
{
return original;
}
public Type ReturnedType
{
get
{
return typeof(Nullable<DateTime>);
}
}
public NHibernate.SqlTypes.SqlType[] SqlTypes
{
get
{
return new[] { new SqlType(System.Data.DbType.DateTime) };
}
}
}
マッピングは次のようになります。
Map(x => x.CreatedTimestamp)
.Column("CreatedTimeStamp")
.CustomType<UTCTimeStamp>()
.Not.Nullable();
データベースは-infinty
レコードの値を取得しています(PostgreSQL)。何が間違っているのかわかりません。誰かが私を正しい方向に向けることができますか?