I am trying to use the .Net 2.0 Nullable datetime type with Nhibernate 1.0.4.0, as per details below. I get an invalid cast exception. Am I correct in thinking this should work? The documentation hints that you may be able to
Quote:
Nullables makes it possible to use nullable base types in NHibernate. Note that .NET 2.0 has this feature.
When I use the NullableDateTimeType supplied in the contrib package in my class it works fine.
If I have to use the nullables from contrib, that's fine - but is there a nullable type for SqlDateTime?
Finally, thanks for making a great tool - looking forward to the release of 1.2!
regards,
av
Hibernate version:1.0.4.0
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Ecs.App.Informer, Ecs.App" table="Informer">
<id name="Id" column="InformerId">
<generator class="assigned" />
</id>
<property name="Description" column="Description" />
<property name="EffectiveFrom" column="EffectiveFrom" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"/>
<property name="EffectiveTo" column="EffectiveTo" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
ISession session = NHibernateSessionManager.Instance.GetSession();
Informer inf = new Informer();
inf.Id = Guid.NewGuid().ToString().Substring(0, 10);
inf.Description = "NUnit Test For Id-" + inf.Id ;
inf.EffectiveFrom = DateTime.Now;
inf.EffectiveTo = null;
session.Save(inf);
NHibernateSessionManager.Instance.GetSession().Flush();
Full stack trace of any exception that occurs:Code:
NHibernate.ADOException: could not insert: [Ecs.App.Informer#d063306c-e] ---> System.InvalidCastException: Specified cast is not valid.
at Nullables.NHibernate.NullableDateTimeType.Set(IDbCommand cmd, Object value, Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Dehydrate(Object id, Object[] fields, Boolean[] includeProperty, IDbCommand st, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
--- End of inner exception stack trace ---
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
at NHibernate.Impl.SessionImpl.Flush()
at Ecs.App.NUnit.InformersTest.TestCreate() in C:\Dev\Ecs\ECS.App\NUnit\InformersTest.cs:line 46
POCO:Code:
public class Informer : AuditTaggedBusinessObject<string>
{
private DateTime? _effectiveFrom;
private DateTime? _effectiveTo;
private string _decription;
public DateTime? EffectiveFrom
{
get
{
return _effectiveFrom;
}
set
{
_effectiveFrom = value;
}
}
public DateTime? EffectiveTo
{
get
{
return _effectiveTo;
}
set
{
_effectiveTo = value;
}
}
public bool IsEffective
{
get { throw new Exception("The method or operation is not implemented."); }
}
/// <summary>
/// The name of the informer.
/// </summary>
public string Description
{
get
{
return _decription;
}
set
{
_decription = value;
}
}
}
Name and version of the database you are using:
SQL Server 2005
The generated SQL (show_sql=true):
None generated