I am new to NHibernate and have difficulty with a simple but well-rounded error.
I have a table in my database (MSSQL2008) where the composite key consists of two date columns.
They represent a StartDate and EndDate time period that is unique for the purposes of my solution.
The table definition is as follows:
CREATE TABLE [dbo]. [CompositeKeyTab] ([KeyCol1] [date] NOT NULL, [KeyCol2] [date] NOT NULL, [Value] [decimal] (18, 0) NULL, CONSTRAINT [PK_CompositeKeyTab] BASIC KEYBOARDS ([KeyCol1] ASC, [KeyCol2] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
In my domain model, I have a corresponding object:
public class CompositeKeyEnt
{
public virtual DateTime KeyCol1 { get; set; }
public virtual DateTime KeyCol2 { get; set; }
public virtual decimal Val { get; set; }
public override bool Equals(object obj)
{
var compareTo = obj as FinancialDay;
if (compareTo == null)
return false;
return this.GetHashCode() == compareTo.GetHashCode();
}
public override int GetHashCode()
{
return this.KeyCol1.GetHashCode() ^ this.KeyCol2.GetHashCode();
}
}
:
public class CompositeKeyEntMap: ClassMap<CompositeKeyEnt>
{
public CompositeKeyEntMap()
{
WithTable("CompositeKeyTab");
UseCompositeId().WithKeyProperty(e => e.KeyCol1, "KeyCol1").WithKeyProperty(e => e.KeyCol2, "KeyCol2");
Map(e => e.Val, "Value");
}
}
. ()
CompositeKeyEnt cke = new CompositeKeyEnt() { KeyCol1 = DateTime.Now.AddDays(1), KeyCol2=DateTime.Now.AddDays(1), Val = 2.2M };
CompositeKeyEnt cke1 = new CompositeKeyEnt() { KeyCol1 = DateTime.Now, KeyCol2 = DateTime.Now, Val = 1.1M };
Repository<CompositeKeyEnt> crep = new Repository<CompositeKeyEnt>();
crep.SaveOrUpdate(cke);
crep.SaveOrUpdate(cke1);
:
" : 0; : 1"
Flush().
public virtual T SaveOrUpdate(T entity)
{
using (var context = Session)
{
context.SaveOrUpdate(entity);
context.Flush();
}
return entity;
}
?