gpt4 book ai didi

c# - 城堡 ActiveRecord : Map to IUserType wihtin Class in C#

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:20 27 4
gpt4 key购买 nike

对于我当前的项目,我在 C# 中使用 CaSTLe 的 ActiveRecord。对于我的一张表,我确实需要使用自定义类型类(处理愚蠢的时间到时间跨度的转换)。为了保持我的代码干净,我喜欢在对象映射类中定义从 IUserType 派生的类。但是我找不到使用这个子类映射这个属性的方法,ActiveRecord 一直在提示:Could not determine type for (...)

这是一个小样本:

namespace testForActiveRecord
{
[ActiveRecord("[firstTable]")]
public class firstTable:ActiveRecordBase<firstTable>
{
private TimeSpan _TStest;

// more private fields and properties here

[Property(ColumnType = "testForActiveRecord.firstTable.StupidDBTimeSpan, testForActiveRecord")]
public TimeSpan TStest
{
get { return _TStest; }
set { _TStest = value; }
}

// Usertype doing the conversion from a date saved in the DB to the timespan it is representing
// The TimeSpan is saved by an offset to the date 30.12.1899...
public class StupidDBTimeSpan : IUserType
{
#region IUserType Member

DateTime Basis = new DateTime(1899,12,30,00,00,00);

object IUserType.Assemble(object cached, object owner)
{
return cached;
}

object IUserType.DeepCopy(object value)
{
return value;
}

object IUserType.Disassemble(object value)
{
return value;
}

bool IUserType.Equals(object x, object y)
{
if (x == y) return true;
if (x == null || y == null) return false;
return x.Equals(y);
}

int IUserType.GetHashCode(object x)
{
return x.GetHashCode();
}

bool IUserType.IsMutable
{
get { return false; }
}

public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
{
object obj = NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
TimeSpan Differenz = new TimeSpan();
if (obj != null)
{
Differenz = (DateTime)obj - Basis;
}
return Differenz;
}

public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
if (value == null)
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
NHibernateUtil.DateTime.NullSafeSet(cmd, Basis + (TimeSpan)value, index);
}
}

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

Type IUserType.ReturnedType
{
get { return typeof(TimeSpan); }
}

NHibernate.SqlTypes.SqlType[] IUserType.SqlTypes
{
get { return new SqlType[] { new SqlType(DbType.DateTime) }; }
}

#endregion
}
}
}

如果 StupidDBTimeSpan 类是在 testForActiveRecord 类之外定义的,并且我正在使用 [Property(ColumnType = "testForActiveRecord.StupidDBTimeSpan , testForActiveRecord")].

我做错了什么?是否可以将此子类构造与 ActiveRecord 一起使用?

问候sc911

最佳答案

因为 StupidDBTimeSpan 是一个内部类,所以 CLR 类型名称是:

testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord

关于c# - 城堡 ActiveRecord : Map to IUserType wihtin Class in C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2492555/

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