gpt4 book ai didi

nhibernate - NHibernate IUserType将可为空的DateTime转换为DB非空值

转载 作者:行者123 更新时间:2023-12-04 18:31:04 28 4
gpt4 key购买 nike

我有旧版数据库,存储的日期表示无日期为9999-21-31,
Till_Date的类型为DateTime not-null="true"

在应用程序中,我要构建持久化类,将no-date表示为null,
所以我在C#// public DateTime中使用了可为空的DateTime? TillDate {获取;组; }

我创建了IUserType,它知道将实体null值转换为DB 9999-12-31

但似乎NHibernate不会在我的IUserType上调用SafeNullGet,SafeNullSet
当实体值为null且报告为null时,用于非null列。

我试图通过将列映射为not-null =“ false”来绕过它
(仅更改了映射文件,而不更改了数据库)
但是它仍然没有帮助,只是现在它尝试将null值插入数据库并获取ADOException。

是否知道NHibernate不支持将null转换为非null值的IUseType?

//实施所需的修复程序!!!

public class NullableDateTimeToNotNullUserType : IUserType
{
private static readonly DateTime MaxDate = new DateTime(9999, 12, 31);

public new bool Equals(object x, object y)
{ //This didn't work as well
if (ReferenceEquals(x, y)) return true; //if(x == null && y == null) return false;

if (x == null || y == null) return false;

return x.Equals(y);
}

public int GetHashCode(object x)
{
return x == null ? 0 : x.GetHashCode();
}

public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = rs.GetDateTime(rs.GetOrdinal(names[0]));

return (value == MaxDate)? null : value;
}

public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var dateValue = (DateTime?)value;

var dbValue = (dateValue.HasValue) ? dateValue.Value : MaxDate;

((IDataParameter)cmd.Parameters[index]).Value = dbValue;
}

public object DeepCopy(object value)
{
return value;
}

public object Replace(object original, object target, object owner)
{
return original;
}

public object Assemble(object cached, object owner)
{
return cached;
}

public object Disassemble(object value)
{
return value;
}

public SqlType[] SqlTypes
{
get { return new[] { NHibernateUtil.DateTime.SqlType }; }
}

public Type ReturnedType
{
get { return typeof(DateTime?); }
}

public bool IsMutable
{
get { return false; }
}
}
}


//带有修复程序的最终实现。

//Make the column mapping in hbm.xml not-null="false" even if in DB null not allowed.
//Make sure the class mapping in xml doesn't have dynamic-insert="true"

public class NullableDateTimeToNotNullUserType : IUserType
{
private static readonly DateTime MaxDate = new DateTime(9999, 12, 31);

public new bool Equals(object x, object y)
{ //This didn't work as well
if (ReferenceEquals(x, y)) return true; //if(x == null && y == null) return false;

if (x == null || y == null) return false;

return x.Equals(y);
}

public int GetHashCode(object x)
{
return x == null ? 0 : x.GetHashCode();
}

public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = NHibernateUtil.Date.NullSafeGet(rs, names[0]);

return (value == MaxDate)? default(DateTime?) : value;
}

public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var dateValue = (DateTime?)value;

var dbValue = (dateValue.HasValue) ? dateValue.Value : MaxDate;

NHibernateUtil.Date.NullSafeSet(cmd, valueToSet, index);
}

public object DeepCopy(object value)
{
return value;
}

public object Replace(object original, object target, object owner)
{
return original;
}

public object Assemble(object cached, object owner)
{
return cached;
}

public object Disassemble(object value)
{
return value;
}

public SqlType[] SqlTypes
{
get { return new[] { NHibernateUtil.DateTime.SqlType }; }
}

public Type ReturnedType
{
get { return typeof(DateTime?); }
}

public bool IsMutable
{
get { return false; }
}
}
}

最佳答案

您实施Equals时可能会出现问题。 NHibernate使用Equals来确定是否需要获取或设置该值。请发布您的IUserType实现。

编辑:好的,我想我看到了问题。试试这个:

public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = NHibernateUtil.Date.NullSafeGet(rs, names[0]);
return (value == MaxDate) ? null : value;
}

public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var dateValue = (DateTime?)value;
var dbValue = (dateValue.HasValue) ? dateValue.Value : MaxDate;
NHibernateUtil.Date.NullSafeSet(cmd, valueToSet, index);
}


NullSafeGet可能还需要将返回值转换为DateTime?。您实施的Equals看起来不错。

关于nhibernate - NHibernate IUserType将可为空的DateTime转换为DB非空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2513986/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com