I am completely new to the whole concept of ORM, NHibernate, and FluentNH, and I am trying to accomplish something that seems so simple ...
I am trying to get an object that has a field defined as Int64 in it. This field will be the identifier defined in the map file.
When trying to retrieve a record from the database, NHibernate returns the following error message: "An identifier of the wrong type was provided. Expected: System.Int32, received 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 as follows:
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 structure of the table (generated by NH) is as follows:
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'm trying to fetch a 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);
What am I doing wrong? What is missing? When deleting a table and redefining my object using Int32 for ID, everything works! I need to use a larger type!
Many thanks!
EDIT 1: As Aaronaught is mentioned, I agree, my need for Int64 is a bit of an overhead for storing holidays ... But for a second, let me forget the Holidays concept. What will I do with my logging table (from 5 to 10 events (rows) per second!). Thanks!
EDIT 2: @Paco: I am using NHibernate 2.1.2.4000 and FluentNH 1.0.0.614
EDIT 3: After reusing Daniel Schilling's solution, rebuilt the (new) simple Holiday object that used Int64 as an identifier. I managed to get a record from the database successfully. As soon as the relation to the Group object was added , when I started the Holiday object, I got the same error message as before ... Here is the HolidayGroup class and map, if 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 ()}