I have the following table in my iSeries DB2 schema:
CREATE TABLE LATHAM1.SGINES1.LICVP010
(ID FOR COLUMN ID10 BIGINT NOT NULL PRIMARY KEY,
CompanyCode FOR COLUMN CONO10 CHARACTER (2) CCSID 37 NOT NULL,
CreateDate FOR COLUMN CDAT10 DATE not null);
and the following mapping for the table:
public class OrderReferenceID
{
public virtual long ID { get; set; }
public virtual string CompanyName { get; set; }
public virtual DateTime CreationDate { get; set; }
}
public class OrderReferenceIDMap : ClassMap<OrderReferenceID>
{
public OrderReferenceIDMap()
{
Table("LICVP010");
Id(x => x.ID, "ID10")
.GeneratedBy.Native()
.Not.Nullable();
Map(x => x.CompanyName, "CONO10")
.Length(2)
.Not.Nullable();
Map(x => x.CreationDate, "CDAT10");
}
}
and the following code:
ISessionFactory sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
OrderReferenceID newID = new OrderReferenceID { CompanyName = "LI", CreationDate = DateTime.Now.Date };
session.Save(newID);
session.Flush();
session.Close();
}
When I execute this, I get an error:
A conversion error occurred.
This appears to be happening because of the conversion from CLR DateTime to DB2 Date object.
How can I properly map this?
I found that modifying this line
Map(x => x.CreationDate, "CDAT10");
to this
Map(x => x.CreationDate, "CDAT10")
.CustomType("Date");
fixed the problem.