NHibernate Invalid Cast Exception... DateTime

949 views
Skip to first unread message

dacanetdev

unread,
Mar 7, 2009, 10:07:16 AM3/7/09
to nhusers
I'm getting this error, could you please help me with this?

The problem is with the property FechaCreacion that it's a DateTime. I
tried with Timestamp and also as string, still getting the error.

Thanks for your help

This is the error

NHibernate.PropertyAccessException: Invalid Cast (check your mapping
for property type mismatches); setter of
JCES.ControlObra.Entities.Obra ---> System.InvalidCastException:
Specified cast is not valid.
at (Object , Object[] , SetterCallback )
at NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues
(Object target, Object[] values)
at
NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer
(Object entity, Object[] values)


This is de Mapping file

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="JCES.ControlObra.Entities"
namespace="JCES.ControlObra.Entities">
<class
name="JCES.ControlObra.Entities.Obra,JCES.ControlObra.Entities"
table="Obra">
<id name="ObraId" column="ObraId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="NumObra" column="NumObra" type="string" length="50"
not-null="true" unique="true" />
<property name="NombreObra" column="NombreObra" type="string"
length="400" not-null="true" />
<property name="FechaCreacion" column="FechaCreacion"
type="DateTime" not-null="true" />
<property name="CostoEstatal" column="CostoEstatal" type="Decimal"
not-null="false" />
<property name="CostoFederal" column="CostoFederal" type="Decimal"
not-null="false" />
<property name="CostoOtros" column="CostoOtros" type="Decimal" not-
null="false" />

<many-to-one name="Municipio" class="Municipio"
column="MunicipioId" />
<many-to-one name="Localidad" class="Localidad"
column="LocalidadId" />
<many-to-one name="TipoObra" class="TipoObra" column="TipoObraId" /
>
</class>
</hibernate-mapping>

This is the class

public class Obra
{

private int obraId;
private string numObra;
private string nombreObra;
private TipoObra tipoObra;
private DateTime fechaCreacion;
private Localidad localidad;
private Municipio municipio;
private double costoEstatal;
private double costoFederal;
private double costoOtros;

public Obra()
{

}

public virtual int ObraId
{
get
{
return obraId;
}
set
{
obraId = value;
}
}

public virtual string NumObra
{
get
{
return numObra;
}
set
{
numObra = value;
}
}

public virtual string NombreObra
{
get
{
return nombreObra;
}
set
{
nombreObra = value;
}
}

public virtual TipoObra TipoObra
{
get
{
return tipoObra;
}
set
{
tipoObra = value;
}
}

public virtual Municipio Municipio
{
get
{
return municipio;
}
set
{
municipio = value;
}
}

public virtual Localidad Localidad
{
get
{
return localidad;
}
set
{
localidad = value;
}
}

public virtual DateTime FechaCreacion
{
get
{
return fechaCreacion;
}
set
{
fechaCreacion = value;
}
}

public virtual double CostoEstatal
{
get
{
return costoEstatal;
}
set
{
costoEstatal = value;
}
}

public virtual double CostoFederal
{
get
{
return costoFederal;
}
set
{
costoFederal = value;
}
}

public virtual double CostoOtros
{
get
{
return costoOtros;
}
set
{
costoOtros = value;
}
}

}//end Obra

Ken Egozi

unread,
Mar 7, 2009, 10:20:25 AM3/7/09
to nhu...@googlegroups.com
is it possible that you have a null value in the DB? (so it can't get casted to dateTime?)
if so - either fix the DB (add NOT NULL constraint and fix the data) or use a nullable datetime for the property

dacanetdev

unread,
Mar 7, 2009, 10:26:10 AM3/7/09
to nhusers
No I don't have a NULL I have only 1 record with this value 07/03/2009
07:56:51 a.m.

I read that the problem is that NHibernate can't get miliseconds
therefore it can't cast to DateTime. As I said I tried with Timestamp
with same result.

Thanks,

On Mar 7, 9:20 am, Ken Egozi <egoz...@gmail.com> wrote:
> is it possible that you have a null value in the DB? (so it can't get casted
> to dateTime?)
> if so - either fix the DB (add NOT NULL constraint and fix the data) or use
> a nullable datetime for the property
>

James Gregory

unread,
Mar 7, 2009, 10:35:50 AM3/7/09
to nhu...@googlegroups.com
What is the type of your column?

Ken Egozi

unread,
Mar 7, 2009, 10:37:45 AM3/7/09
to nhu...@googlegroups.com
(in the DB)

dacanetdev

unread,
Mar 7, 2009, 11:23:07 AM3/7/09
to nhusers
The type originally is datetime I also tried changing it to
smalldatetime, didn't work. I'm really desesperate need to deliver
some results I tried to change the property to be string but same bad
result.

Any help would be appreciated.

Thanks,

On Mar 7, 9:35 am, James Gregory <jagregory....@gmail.com> wrote:
> What is the type of your column?
>
> >http://www.kenegozi.com/bloghttp://www.delver.comhttp://www.musicglue...

Fabio Maulo

unread,
Mar 7, 2009, 12:04:48 PM3/7/09
to nhu...@googlegroups.com
"URGENT" is something I don't understand if it is not accomplished with money.

2009/3/7 dacanetdev <iscdavid...@gmail.com>



--
Fabio Maulo

James Gregory

unread,
Mar 7, 2009, 12:09:14 PM3/7/09
to nhu...@googlegroups.com
Please don't change the subject of your email mid-thread, it throws off email clients.

dacanetdev

unread,
Mar 7, 2009, 12:17:01 PM3/7/09
to nhusers
Sorry, I don't want to bother you Fabio, just I'm really desesperate
and now I can't get that fixed and I will have to go back to Data
centric approach that I wouldn't want to do. I have my project with
too much tables with DateTime columns

Sorry if I changed the subject I just saw the option and I thought it
is ok to use that functionality.

Thanks,

On Mar 7, 11:04 am, Fabio Maulo <fabioma...@gmail.com> wrote:
> "URGENT" is something I don't understand if it is not accomplished with
> money.
>
> 2009/3/7 dacanetdev <iscdavidcarri...@gmail.com>

James Gregory

unread,
Mar 7, 2009, 1:00:34 PM3/7/09
to nhu...@googlegroups.com
I may have missed something, but what makes you think it's the DateTime field? I can't see anything in the exception that mentions it.

Have you tried reducing your table and model (removing fields/properties) until you're able to stop it from occurring?
Reply all
Reply to author
Forward
0 new messages