views:

181

answers:

2

I'm enjoying using Fluent NHibernate with AutoMappings but every now and then I get this runtime error when trying to create the fluent configuration: 'An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collections, and InnerException for more detail.'

Now this can mean all sorts of things, but the exception details are not very helpful at all. Sometimes the class that NHibernate fails to process will be listed in the InnerException, other times I get mystic messages like this:

{"(XmlDocument)(3,6): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'property' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'."}

That doesnt really help me track down the error. Is there any way to get a detailed log of what NHibernate's trying to do (ie, what class/method it's currently auto-mapping) so I can at least identify the part of my code that is causing the problem?

edit:

Known reasons for this error:

  • database connectivity (should be visible in InnerException detail)
  • missing virtual modifier on properties (should indicate type in InnerException detail)
  • missing ID field (gives non specific error as listed above)
+3  A: 

Welcome to the wonderful world of NHibernate debugging! I find the the lack of meaningful error messages one of the most annoying aspects of working with NHibernate. Pretty cool when it works, however.

This is a wild guess, but I've seen this message when I've forgotten to add an "Id" property to one of the classes I'm trying to map.

If you read the error message with this in mind, you'll see that this is one of the things that's potentially missing.

Tom Bushell
Yeah, I've got a whole checklist - I think i'll add them to the original post for the sake of the next person to search for this error.
Alex
You were right - I'd forgotten to derive from the abstract class with my identifier. I've given you a point but not marked it as answered, as my fundamental question is trying to find a better way to approach fluent configuration debugging. Thanks for your help!
Alex
Yes, can be very annoying
UpTheCreek
Once you've worked with FNH for a while, you start to recognize the common errors, and it get's easier. So hang in there - it's worth it!
Tom Bushell
+2  A: 

Here's a link that might be useful - debugging nhibernate exceptions. There's mention of setting up logging, but I have not tried it myself.

If you try it, please let us know how you make out.

Another helpful link - troubleshooting common mapping bugs with nhibernate

Tom Bushell
NHProfiler looks good, but I'm baulking at the price. I'll try the log4net solution and report back.
Alex
Logging helps, though I think I've gotten to the point now where I can decrypt most of the exceptions.
Alex
I'd be surprised if logging helps much here. I had this error, and just found the same thing in the NHibernate log - no further clues. And it's ridiculous that you would have to rely on a third party product to find out what in the configuration is causing this error.
UpTheCreek
That second link is really helpfuly though +1
UpTheCreek
Glad it helped. BTW, since I wrote the above, I've gotten log4net working, and it has helped me fix at least one problem because it provides somewhat more detail than the exception traces do. Worth a try.
Tom Bushell