How to add new object to an IList mapped as a one-to-many with NHibernate?
- by Jørn Schou-Rode
My model contains a class Section which has an ordered list of Statics that are part of this section. Leaving all the other properties out, the implementation of the model looks like this:
public class Section
{
public virtual int Id { get; private set; }
public virtual IList<Static> Statics { get; private set; }
}
public class Static
{
public virtual int Id { get; private set; }
}
In the database, the relationship is implemented as a one-to-many, where the table Static has a foreign key pointing to Section and an integer column Position to store its index position in the list it is part of.
The mapping is done in Fluent NHibernate like this:
public SectionMap()
{
Id(x => x.Id);
HasMany(x => x.Statics).Cascade.All().LazyLoad()
.AsList(x => x.WithColumn("Position"));
}
public StaticMap()
{
Id(x => x.Id);
References(x => x.Section);
}
Now I am able to load existing Statics, and I am also able to update the details of those. However, I cannot seem to find a way to add new Statics to a Section, and have this change persisted to the database. I have tried several combinations of:
mySection.Statics.Add(myStatic)
session.Update(mySection)
session.Save(myStatic)
but the closest I have gotten (using the first two statements), is to an SQL exception reading: "Cannot insert the value NULL into column 'Position'". Clearly an INSERT is attempted here, but NHibernate does not seem to automatically append the index position to the SQL statement.
What am I doing wrong? Am I missing something in my mappings? Do I need to expose the Position column as a property and assign a value to it myself?
EDIT: Apparently everything works as expected, if I remove the NOT NULL constraint on the Static.Position column in the database. I guess NHibernate makes the insert and immediatly after updates the row with a Position value.
While this is an anwers to the question, I am not sure if it is the best one. I would prefer the Position column to be not nullable, so I still hope there is some way to make NHibernate provide a value for that column directly in the INSERT statement.
Thus, the question is still open. Any other solutions?