<p>I have an NHibernate session. In this session, I am performing exactly 1 operation, which is to run this code to get a list:</p>

<pre><code>public IList&lt;Customer&gt; GetCustomerByFirstName(string customerFirstName)
{
return _session.CreateCriteria(typeof(Customer))
    .Add(new NHibernate.Expression.EqExpression("FirstName", customerFirstName))
    .List&lt;Customer&gt;();
}
</code></pre>

<p>I am calling <code>Session.Flush()</code> at the end of the <code>HttpRequest</code>, and I get a <code>HibernateAdoException</code>. NHibernate is passing an update statement to the db, and causing a foreign key violation. If I don't run the <code>flush</code>, the request completes with no problem. The issue here is that I need the flush in place in case there is a change that occurs within other sessions, since this code is reused in other areas. Is there another configuration setting I might be missing?</p>

<hr>

<p>Here's the code from the exception:</p>

<pre><code>[SQL: UPDATE CUSTOMER SET first_name = ?, last_name = ?, strategy_code_1 = ?, strategy_code_2 = ?, strategy_code_3 = ?, dts_import = ?, account_cycle_code = ?, bucket = ?, collector_code = ?, days_delinquent_count = ?, external_status_code = ?, principal_balance_amount = ?, total_min_pay_due = ?, current_balance = ?, amount_delinquent = ?, current_min_pay_due = ?, bucket_1 = ?, bucket_2 = ?, bucket_3 = ?, bucket_4 = ?, bucket_5 = ?, bucket_6 = ?, bucket_7 = ? WHERE customer_account_id = ?]
</code></pre>

<p>No parameters are showing as being passed.</p>

[{'answer_id': 34965, 'author': 'Ryan Duffield', 'author_id': 2696, 'author_profile': 'https://Stackoverflow.com/users/2696', 'pm_score': 5, 'selected': True, 'text': "<p>I have seen this once before when one of my models was not mapped correctly (wasn't using nullable types correctly).  May you please paste your model and mapping?</p>\n"}, {'answer_id': 1146711, 'author': 'Richard Dingwall', 'author_id': 91551, 'author_profile': 'https://Stackoverflow.com/users/91551', 'pm_score': 0, 'selected': False, 'text': '<p>I also experienced this problem in NH 2.0.1 when trying to hide the inverse ends of many-to-many bags using access="noop" (hint: this doesn\'t work). </p>\n\n<p>Converting them to access="field" + adding a field on the class fixed the problem. Pretty hard to track them down though.</p>\n'}, {'answer_id': 1434275, 'author': 'Andriy Volkov', 'author_id': 76859, 'author_profile': 'https://Stackoverflow.com/users/76859', 'pm_score': 6, 'selected': False, 'text': '<p>Always be careful with NULLable fields whenever you deal with NHibernate. If your field is NULLable in DB, make sure corresponding .NET class uses Nullable type too. Otherwise, all kinds of weird things will happen. The symptom is usually will be that NHibernate will try to update the record in DB, even though you have not changed any fields since you read the entity from the database.</p>\n\n<p>The following sequence explains why this happens:</p>\n\n<ol>\n<li>NHibernate retrieves raw entity\'s data from DB using ADO.NET</li>\n<li>NHibernate constructs the entity and sets its properties</li>\n<li>If DB field contained NULL the property will be set to the defaul value for its type:\n\n<ul>\n<li>properties of reference types will be set to null</li>\n<li>properties of integer and floating point types will be set to 0</li>\n<li>properties of boolean type will be set to false</li>\n<li>properties of DateTime type will be set to DateTime.MinValue</li>\n<li>etc.</li>\n</ul></li>\n<li>Now, when transaction is committed, NHibernate compares the value of the property to the original field value it read form DB, and since the field contained NULL but the property contains a non-null value, <strong>NHibernate considers the property dirty, and forces an update of the enity.</strong></li>\n</ol>\n\n<p>Not only this hurts performance (you get extra round-trip to DB and extra update every time you retrieve the entity) but it also may cause hard to troubleshoot errors with DateTime columns. Indeed, when DateTime property is initialized to its default value it\'s set to 1/1/0001. When this value is saved to DB, ADO.NET\'s SqlClient can\'t convert it to a valid SqlDateTime value since the smallest possible SqlDateTime is 1/1/1753!!!</p>\n\n<p>The easiest fix is to make the class property use Nullable type, in this case "DateTime?". Alternatively, you could implement a custom type mapper by implementing IUserType with its Equals method properly comparing DbNull.Value with whatever default value of your value type. In our case Equals would need to return true when comparing 1/1/0001 with DbNull.Value. Implementing a full-functional IUserType is not really that hard but it does require knowledge of NHibernate trivia so prepare to do some substantial googling if you choose to go that way.</p>\n'}]
['https://Stackoverflow.com/questions/34852', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1284/']