VerySimple Developer Blog
Technical Tips, Tricks and Rants.
 
Jul
08
Filed Under (.NET, Announcements) by Jason on 08-07-2007

The dreaded “NHibernate.MappingException: Unknown entity class” exception thrown by NHibernate in your .NET web application indicates that the class you are trying to map in your .hbm.xml file cannot be located for some reason.

The first thing to check is to right-click on each of your .hbm.xml files and make sure the Build Action property is set to “Embeded Resource.” This tells .NET to compile the mapping file into the Assembly .dll

If you confirmed that the Build Action is correct then for some reason NHibernate cannot locate your class within the project. Double check that the path to your class is correct. If you’re building a .NET 2.0 website and your model code is inside the App_Code directory, make sure that you have specified the Assembly name of “App_Code”. The .NET framework automatically creates this assembly name for your web project.

 

Comments:
26 Comments posted on "NHibernate.MappingException: Unknown entity class"
Olavo Alexandrino on July 19th, 2007 at 8:07 pm #

Thanks a lot by your post!

Was a shame discover that!

;)


Jam on July 21st, 2007 at 5:50 pm #

Exactly what I suffered! Thanks man u rule!


Ben on August 3rd, 2007 at 2:09 pm #

Maybe I am a little slow. I have the same issue but, I am unable to get this working even when I include the changes you discuss above. I am referencing the App_Code directory without any success. Can anyone be more specific about referencing components in the App_Code folder? Any assistance is greatly appreciated.


koresh on September 17th, 2007 at 3:03 am #

Hi from Russia!)
I specified the Assembly name of “App_Code” but i have this issue again…
this is my .hbm.xml

in web.congig i set up


Jason on September 17th, 2007 at 12:14 pm #

Hey Ben & Koresh - One thing that I did when I was having some similar problems was create a little bit of debug code to print out the assembly name of one of my classes in question so i could see what it was. There is a method to do that somewhere in the Reflection class, sorry I don’t remember off the top of my head.


Rui on October 13th, 2007 at 1:44 pm #

Try to add the following line in configuration file inside the (session-factory) tab:

(mapping resource=”«namespace».«hbm.xml file” assembly=”namespace” /)

Example:

(mapping resource=”Quickstart.Cat.hbm.xml” assembly=”Quickstart” /)

Replace the () by the correct character because this blog doesnt accept those


Ronald on October 25th, 2007 at 6:16 am #

Hi from South Africa.

I have the same problem, but not with ASP.net. I am using a normal application. does anyone know what to do here?

Ronald


Meziano on October 25th, 2007 at 8:02 am #

Hey all,
the 2 Hibernate Mappingfiles properties: ‘Buildvorgang’ and ‘In ausgabeverzeichnis kopieren’ (sorry I have a German VS, the 2 & 3 properties) must be set of ‘Eingebetete Ressource’ and ‘immer kopieren’.
That’s it!


Jason on October 28th, 2007 at 5:50 pm #

Hey Ronald - with a normal application it most likely is the same thing where the assembly name is not right or else you don’t have the mapping files set to be “embedded resource” - try to use the reflection classes or the debugger to figure out what the assembly name is for your objects that you are trying to map.


Alexandre on November 26th, 2007 at 3:37 pm #

Thanks man! I’m just starting with NHibernate on VS2005. I worked just embedding the XML file.


Paul on March 9th, 2008 at 5:36 pm #

In visual studio 2005 how do you embed the resource the property panel only gives file name and full path as the two properties for my xml file, im hunting high and low to find this for VS 2005.


frc on March 11th, 2008 at 9:21 am #

Try naming the namespace of the project you’re using different as the NHibernate or at least the property of the assembly name to be different. After that set class props to virtual. Hope that makes sense. Worked here! Cheers!
P.S. Embeded resource needed also on the xml mapped file :)


Avinash Joshi on March 11th, 2008 at 10:48 pm #

thanks My friend you help a Lot


Michael Hale on March 24th, 2008 at 1:52 pm #

Another reason for the Unknown entity class error is when you have the same property defined in 2 different ways in your hbm file. It seems like this should give you a different type of error.


JWB on March 26th, 2008 at 3:11 am #

And check that your file is correctly named .hbm.xml


Tim Ivashchenko on March 29th, 2008 at 5:40 am #

Thanks! Your explanation resolved my problem. Good luck!


Daniel on April 10th, 2008 at 11:54 am #

Thank you so much !!!
Almoust a whole day trying to find out why it wasn’t working !
Two minutes on your web site and I found the solution !


Lee on April 22nd, 2008 at 6:49 pm #

Thanks for posting this. I ran into the same problem - in addition to the fix you outlined, I also had to add
(mapping assembly=”my_assembly_name”/) to get this to work.


Tomas on May 5th, 2008 at 3:06 am #

Thanks! This solved my problem. :)


Kalpesh on May 9th, 2008 at 8:27 am #

Hey ..u made my day..


Jesús Miasnikoff on May 28th, 2008 at 7:19 pm #

excelent!


Vignesh on May 30th, 2008 at 6:38 am #

Hey !! I have also done a sample in Hibernate ASP.Net application. Its working perfectly. Just make sure the XML file build property is set to ‘Embedded Resource’.


Vignesh on May 30th, 2008 at 6:39 am #

I will try to explore further about the DB insertions, updations etc and sahre it with you guys. If any of you guys have any idea on inserting identity values etc.. please let me know !! Thanks and Happy coding !!!


Sandesh Deshmukh on June 13th, 2008 at 1:27 am #

Thanks a lot bro, this is such a stupid mistake, but one of the most difficult ones to figure out!


Honda on July 2nd, 2008 at 3:32 am #

Thank you!!!!!!!


Prachi on July 2nd, 2008 at 11:04 am #

I am having a similar problem.Can anyone please tell me where in the web.config file I am supposed to add the line??It is not recongnizing the setting…Please reply..thanks a lot in advance :)


Post a comment

Name: 
Email: 
URL: 
Comments: 
Close
  • Social Web

NOTE: Email is disabled

E-mail It