RSS
 

NHibernate.MappingException: Unknown entity class

08 Jul

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.

 
53 Comments

Posted in .NET

 

Leave a Reply

 
Please leave these two fields as-is:
 
  1. Olavo Alexandrino

    July 19, 2007 at 8:07 pm

    Thanks a lot by your post!

    Was a shame discover that!
    ;)

     
  2. Jam

    July 21, 2007 at 5:50 pm

    Exactly what I suffered! Thanks man u rule!

     
  3. Ben

    August 3, 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.

     
  4. koresh

    September 17, 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

     
  5. Jason

    September 17, 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.

     
  6. Rui

    October 13, 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

     
  7. Ronald

    October 25, 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

     
  8. Meziano

    October 25, 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!

     
  9. Jason

    October 28, 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.

     
  10. Alexandre

    November 26, 2007 at 3:37 pm

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

     
  11. Paul

    March 9, 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.

     
  12. frc

    March 11, 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 :)

     
  13. Avinash Joshi

    March 11, 2008 at 10:48 pm

    thanks My friend you help a Lot

     
  14. Michael Hale

    March 24, 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.

     
  15. JWB

    March 26, 2008 at 3:11 am

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

     
  16. Tim Ivashchenko

    March 29, 2008 at 5:40 am

    Thanks! Your explanation resolved my problem. Good luck!

     
  17. Daniel

    April 10, 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 !

     
  18. Lee

    April 22, 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.

     
  19. Tomas

    May 5, 2008 at 3:06 am

    Thanks! This solved my problem. :)

     
  20. Kalpesh

    May 9, 2008 at 8:27 am

    Hey ..u made my day..

     
  21. Jesús Miasnikoff

    May 28, 2008 at 7:19 pm

    excelent!

     
  22. Vignesh

    May 30, 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’.

     
  23. Vignesh

    May 30, 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 !!!

     
  24. Sandesh Deshmukh

    June 13, 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!

     
  25. Honda

    July 2, 2008 at 3:32 am

    Thank you!!!!!!!

     
  26. Prachi

    July 2, 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 :)

     
  27. Chad

    July 10, 2008 at 4:06 am

    Thanks dude. I obviously didn’t read the tuturials closely enough to notice the ‘Embedded Resource’ setting!

     
  28. Naveen

    July 30, 2008 at 2:18 pm

    Hi,

    There could be one more issue if you are not fetching the configuration properly. Below should be the sequence of the statements:

    // Fetch the configuration
    NHibernate.Cfg.Configuration config;
    config = new NHibernate.Cfg.Configuration().Configure();

    // Load the assembly
    config.AddAssembly(“Model”);
    private static ISessionFactory sessionFactory = config.BuildSessionFactory();

    // DEBUG – check count to verify whether all classes are loaded properly
    IDictionary colls = sessionFactory.GetAllClassMetadata();
    int count = colls.Count;

     
  29. Alejandro

    September 3, 2008 at 11:01 am

    Thanks!!

    From Argentina.

     
  30. Solmaz

    September 17, 2008 at 4:51 am

    vaooooooooooooo, tanx very much , I spent 4 hour for that and u save me :)

     
  31. Dharmbir

    September 23, 2008 at 7:04 am

    Hi,
    I am also facing the same problem and I have added all the seeting that you guys have suggested but still it’s giving me the same error “Could not find the dialect in the configuration”

     
  32. Dharmbir

    September 23, 2008 at 7:05 am

    Hi,
    please see my app.config file
    (nhibernate)
    (add key=”hibernate.show_sql”
    value=”false” /)
    (add key=”hibernate.connection.provider” value=”NHibernate.Connection.DriverConnectionProvider” /)
    (add key=”hibernate.dialect” value=”NHibernate.hibernate.Dialect.MsSql2000Dialect” /)
    (add key=”hibernate.connection.driver_class” value=”NHibernate.Driver.SqlClientDriver” /)
    (add key=”hibernate.connection.connection_string” value=”Data Source=sqldev;Initial Catalog=AssetIceTest;IntegratedSecurity=SSPI;” /)
    (mapping resource=”Employee.hbm.xml” assembly=”MyClass” /)

     
  33. Sagar

    September 25, 2008 at 7:49 am

    I’m unable to resolve this issue.. Can anyone please help? I’ve set the “Build Action” to “Embedded Resource and secondly I’ve added the below line in my config file:

    User.hbm.xml is my hbm.xml file and the name my application is NHibernate.

    Thanks in advance

     
  34. Jason

    September 25, 2008 at 11:13 am

    Hey Sagar, not too sure if your project being named NHibernate is a possible problem due to namespace collision..? It’s tough to say, but again, the error is always due to NHibernate not being able to locate your class. So, check your folder paths, assembly name and everything.

    Also, possibly grab the source of NHibernate and put a debugger on it to see what class it is trying to find.

     
  35. Jason

    September 25, 2008 at 11:15 am

    Hey Dharmbir, I don’t think this is the same problem. The error about the Dialect seems to me to indicate a problem with your configuration of the DB type. I haven’t encountered that one yet, but I would be looking into the dialect configuration and database connection settings.

     
  36. Solmaz

    September 27, 2008 at 1:32 am

    Tanx alot, i have a problem and ur solution solved it :)

     
  37. Chris Needham

    November 24, 2008 at 4:48 am

    Thanks fella!

     
  38. Love

    November 30, 2008 at 9:37 am

    Thanks mr. programmer.

     
  39. darnok

    December 31, 2008 at 11:35 am

    Thanks from Poland for “Embeded Resource” tip :)

     
  40. Vivek

    March 20, 2009 at 6:26 am

    Thanks a lot… this post helped me

     
  41. Jesus

    April 30, 2009 at 12:31 am

    thanks a lot man I suffered by that exception
    3 hours:D(JESUS CHRIST)

     
  42. Daniel

    May 15, 2009 at 6:33 am

    Thanks man, sometimes the you just forget to set the embedded ressource…

     
  43. NHibernate.MappingException: Unknown entity class « Kenno’s OpenNote

    May 26, 2009 at 1:11 am

    [...] VerySimple, Inc Tagged with: NHibernate no comments yet « Debian JSON [...]

     
  44. yogesh

    May 26, 2009 at 1:36 am

    hello, i m using asp.net website
    can u tell me how to set build property in asp.net website application…..
    i could not found the build property of xml file here….so please

     
  45. mahdi

    June 4, 2009 at 5:45 am

    realy thank u? ;)

     
  46. asif

    June 13, 2009 at 4:25 am

    A0A
    plz update me how i can
    “Build Action” to “Embedded Resource.
    i m using asp.net 2.0 and i have placed the the code in App_Code folder.
    i m getting unknow entity Cat class.

     
  47. mehdi

    July 2, 2009 at 10:41 pm

    Thank you

     
  48. gash

    July 30, 2009 at 2:04 pm

    Thanks a lot. It didn’t exactly solve my problem but gave me an idea for the solution. I had to ensure that there were no mis-spellings in my mapping files.

     
  49. Suman

    September 4, 2009 at 5:19 am

    Hi,

    I am new to nhibernet. I have created on Console Application and added User Class and corresponding hbm.xml made it embeded resource

    Code

    Configuration cfg = new Configuration();
    cfg.Configure(@”ProjectLocation\hibernate.cfg.xml”);

    it is giving
    NHibernate.MappingException: Could not compile the mapping document: POCNibernet.User.hbm.xml

     
  50. John R

    December 3, 2009 at 12:21 pm

    You just solved my problem! A class that was not recognized in my NHibernate mapping was not set to “Embedded Resource”. Changing that setting fixed my bug. Thanks a bunch!

     
  51. Juan P

    March 17, 2010 at 10:30 am

    Hi from Mexico!
    Thank you so much!!

     
  52. peter

    March 17, 2010 at 3:45 pm

    in web.config

    did it for me

    thanks for this blog

     
  53. Dedeco

    May 12, 2010 at 9:16 pm

    Thanks a lot!! Works for me “set to “Embeded Resource.””.