tags:

views:

324

answers:

1

I am calling Save on an NHibernate object that has many children attached to it. Upon save, sometimes an error happens when NHibernate starts inserting into my sql database ("The data was truncated while converting from one data type to another." for instance). Following are the top lines of an example Exception. NHibernate never tells me more than just...it failed. My question is, how do I figure out which table/column is the culprit within my save error?

at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand (CommandBehavior behavior, String method, ResultSetOptions options) at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) at NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert (SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object [] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object [] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityIdentityInsertAction.Execute()

+1  A: 

The problem is that the error happens in the SqlServerCe library. So NHibernate can't tell you what's wrong.

Most probably you have chosen a datatype in the mapping file that is not the same in the database (eg. you have a number in the model and a varchar in the database). The database performs a implicit cast, and if it fails, you get this error.

You can find it by:

  • watching the values in the debugger and try to find one that is larger then usual
  • reading your mapping file and class declarations and compare it with you database model
  • exporting the model using SchemaExport and compare the exported model with yours.
Stefan Steinegger
OK, I did not explain my question very well. I can easily track down exactly where the database error is happening on my local dev box (turn on NHibernate logging, for instance). The problem occurs when there is a client using the application, and they get a random Save error. I have no idea (from the Exception) which of the 100+ tables it is coming from. I would NHibernate to tell me which exact entity it was flushing when the error occurred.
dvanorny
Can't you see this in the stack trace, what you application is doing when the error occurs?
Stefan Steinegger
I've encountered this issue, although I think it's more about SqlCe than NH. I need to know what field was too long in order to show the user an appropriate error message. The exception raised by SqlCe says nearly nothing.
HappyNomad