Wednesday, September 9, 2009

FluentNHibernate 'urn:nhibernate-mapping-2.2' has invalid child element

Just a quick post for anyone who may be suffering with this error.  If you get an error that looks like this,

NHibernate.MappingException: (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'. --->

then there could be two causes for this depending on how you are configuring FNH.

If you are using FluentMapping which is to say you are explicitly mapping each class then you must map the ID Column differently then the rest of them.  I just upgraded from NH 1. something and it’s concurrent version of FNH and I had the following in my origional mapping  UseIdentityForKey(x=>x.Id, “id”). After upgrading I saw that the UseIdentityForKey no longer existed and having read that there is a convention for discovering the Id column I figured that I’d just map it the same as the other Map(x=>x.Id).  Well that was kind of silly and produced the above error.  So I changed it Id(x=>x.Id) and all was good.

The second case may be a bit more insidious.  If you are using Automapping, FNH will indeed discover your id columns for you.  Yay!  However, only if you have named then Id.  That’s capital I lower case d. If you have it all caps ( or for some reason all lower ) like ID then you will get the above error.

I posted this cuz I didn’t find any other posts on this error.  Which could mean that I’m sooo brilliant that no one has even thought of making the mistakes that I make.  There’s another interpretation which I’m sure you’ve already come up will so I will leave it un uttered.