Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate: How to find responsible Field for SqlDateTime overflow exception

I know the reason for the exception (SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.) is a non nullable DateTime field in a Entity and so Nhibernate wants to save a smaller DateTime value than MSSQL accepts.

The Problem ist that there are far to many entities in the project to find the right DateTime field.

The exception occurs after an SaveOrUpdate() but is not triggered by the entity i want to save but any other entity which was loaded in the current session and now is affected by the flush().

How can i find out which field really is responsible for the exception?

like image 988
Tardis Avatar asked Feb 01 '11 15:02

Tardis


1 Answers

If you cast the exception to a SqlTypeException, that will expose the Data collection. Normally there is a single Key and a single Value in the collection. The value is the SQL that was attempted to be executed. By examining the DML you can then see what table was being acted upon. Hopefully that table is narrow enough to make determining the offending column trivial.

Here's some simple code I use to spit out the Key and Value of the exception.

            catch (SqlTypeException e)
            {
                foreach(var key in e.Data.Keys)
                {
                    System.Console.Write("Key is " + key.ToString());
                }
                foreach(var value in e.Data.Values)
                {
                    Console.WriteLine("Value is "+value.ToString());
                }
            }
like image 190
Bill Gregg Avatar answered Nov 15 '22 10:11

Bill Gregg