How can I determine whether a session is Dirty or not without calling IsDirty, because this method performs a flush. My problem is, that I want to add objects to a list.
Code:
Word word = NHDataManager.LoadById<Word>(35, exchangeContext);
word.WordExpressions.Add(new WordExpression("some new expression", word));
NHDataManager.Save(word, exchangeContext);
The new WordExpression object is not ready for saving yet. In the Save method I want to set the missing values (created user, changed user) by use of reflection. Here I have to determine whether the object (graph) has been changed or not. When I call the IsDirty method, I got the following exception:
Util.Database.NHibernate.NHDataManagerException: could not insert: [Wisdom3000NH
Persistence.Entities.WordExpression][SQL: INSERT INTO [Word_Expression] ([change
d_at], [created_at], [synonym_code], [catalogue], [word_id], [changed_user_id],
[created_user_id]) VALUES (?, ?, ?, ?, ?, ?, ?)] in Util.Database.NHibernate.NHD
ataManager.SetTrackingValues ---> NHibernate.ADOException: could not insert: [Wi
sdom3000NHPersistence.Entities.WordExpression][SQL: INSERT INTO [Word_Expression
] ([changed_at], [created_at], [synonym_code], [catalogue], [word_id], [changed_
user_id], [created_user_id]) VALUES (?, ?, ?, ?, ?, ?, ?)] ---> System.Data.SqlC
lient.SqlException: Cannot insert the value NULL into column 'changed_user_id',
table 'Wisdom3000-Test.dbo.Word_Expression'; column does not allow nulls. INSERT
fails.
How can I avoid this annoying behaviour?
Of course, I could do something like this:
Code:
Word word = NHDataManager.LoadById<Word>(35, exchangeContext);
// Perform in this method the reflection stuff and then add the object to the list
NHDataManager.AddToList(new WordExpression("some new expression", word), word, exchangeContext);
NHDataManager.Save(word, exchangeContext);
This works fine, but it's pretty ugly. Does anyone have any idea?