Hi guys,
I'm quite new to the whole concept of ORM, NHibernate and FluentNH, and i'm trying to accomplish something that seems so simple...
I'm trying to retrieve an object that has a field defined as Int64 in it's class. This field would be the ID, as defined in the Map file.
When trying to retrieve a record from the DB, NHibernate returns the following error message : "Provided id of the wrong type. Expected: System.Int32, got System.Int64"
I have a very simple object :
public class Holiday
{
public virtual Int64 HolidayID { get; set; }
public virtual string Name { get; set; }
public virtual int Day { get; set; }
public virtual int Month { get; set; }
public virtual bool IsActive { get; set; }
public virtual HolidayGroup Group { get; set; }
public Holiday() { }
}
the Mapping file (for FluentNH) is the following :
public class HolidayMap : ClassMap<Holiday>
{
public HolidayMap()
{
Id(x => x.HolidayID);
Map(x => x.Name);
Map(x => x.Day);
Map(x => x.Month);
Map(x => x.IsActive);
HasOne(x => x.Group);
}
}
the table structure (generated by NH) is the following:
CREATE TABLE [dbo].[Holiday](
[HolidayID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](255) NULL,
[Day] [int] NULL,
[Month] [int] NULL,
[IsActive] [bit] NULL,
[HolidayGroup_id] [int] NULL,
PRIMARY KEY CLUSTERED
(
[HolidayID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
and finally, i am trying to retrieve the Holiday instance using this:
public static Holiday Get(Int64 _holidayID)
{
Holiday _holiday = new Holiday();
try
{
using (var session = Global.NHibernate_SessionFactory.OpenSession())
{
_holiday = session.Get<Holiday>(_holidayID); // EXCEPTION OCCURS HERE
}
}
catch (Exception _ex)
{
// TODO : Implement proper logging
}
return _holiday;
}
What am i doing wrong?? What's missing? When deleting the table and redefining my object using an Int32 for the ID, everything works! I need use a bigger type!
Many thanks!
EDIT 1 : As Aaronaught mentions, i agree, my need for an Int64 is a bit of overhead for storing holidays... But for a second, let's forget the "Holidays" concept. What am i going to do with my logging table (5 to 10 events (rows) per seconds!). Thanks!
EDIT 2 : @Paco : I'm using NHibernate 2.1.2.4000 and FluentNH 1.0.0.614
EDIT 3 : After rerading Daniel Schilling's solution, rebuilt a (brand new) simple Holiday object that used an Int64 as an ID. I was able to retrieve a record from the DB successfully. As soon as i added a relationship to a Group object, when instanciating a Holiday object i received the same error message as earlier... Here is the HolidayGroup class and mapping, in case you can tell me what i did wrong...
public class HolidayGroup
{
public virtual int HolidayGroupID { get; set;}
public virtual string Name { get; set; }
public virtual string Notes { get; set; }
public virtual bool IsActive { get; set; }
public virtual IList<Holiday> Holidays { get; set; }
public HolidayGroup()
{
Holidays = new List<Holiday>();
}
}
public HolidayGroupMap() { Id(x => x.HolidayGroupID); Map(x => x.Name); Map(x => x.Notes); Map(x => x.IsActive); HasMany(x => x.Holidays) .Cascade.All() }