NHibernate and Manually Assigned Identifiers
Be careful when you use manually assigned identifiers on your entities, because NHibernate looks at them to determine if an object is new or is already in the database.
For example, suppose you have a parent class A which holds a collection of child class B:
using (ISession session = ...)
using (ITransaction tx = session.BeginTransaction())
{
A a = session.Load<A>(1);
B b = new B();
b.Id = 100;
a.B.Add(b);
b.A = a;
tx.Commit();
}
NHibernate will mistakenly assume that, since b has a non-default id, it is an existing object, and will try to update it, which, of course, will throw an exception, since there is no associated record on the database. In this case, you must explicitly save the b object.
}