Search code examples
c#nhibernatenhibernate-mapping

NHibernate Mapping Error with v4.0


I am using VS 2013 32 bit, nHibernate v4.0.0.4000. I am new to nHiberNate and is there anyway I can avoid mapping creation without using fluent. I need good working sample which I could not find so far.

I am getting this error for below code.

Additional information: Could not compile the mapping document: C:\Project...\Some\Models\NHibernate\Mappings\SomeInfo.hbm.xml

InnerException: {"Problem trying to set property type by reflection"}

Code & Mapping -----

My class:
namespace Some.Models
{
    public class SomeInfo 
    {
        public long     Form_Id { get; set; }   
        public string   Account_Id { get; set; } 
        public char     Entity { get; set; }
        public string   Name { get; set; }
        public decimal  Ownership_Percent { get; set; } 
    }
}

Mapping Instance Creation:

namespace Some.Models
{
    public class Hibernate_Connection
    {
        ISessionFactory sessionFactory;        
        ISession OpenSession()
        {
            if (sessionFactory == null)
            {
                var cgf = new Configuration();
                var data = cgf.Configure(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Configuration\hibernate.cfg.xml"));
                cgf.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Mappings")));
                sessionFactory = data.BuildSessionFactory();
            }
            return sessionFactory.OpenSession();
        }

Mapping :Models/NHiberNate/Mappings/SomeInfo.hbm.xml

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping 
xmlns="urn:nhibernate-mapping-2.2" 
assembly="Some" 
namespace="Some.Models" 
auto-import="true"> 
<class name="SomeInfo" table="Some_Table" lazy="false"> 
  <!--<id name="ID" column="Form_Id" Type="Long">
       <generator class="native"></generator> </id>-->
  <id name="ID" column="Form_Id"> 
     <generator class="native"></generator> 
  </id>
  <property name="Form_Id" column="Form_Id"></property> 
  <property name="Account_Id" column="Account_Id"></property> 
  <property name="Entity" column="Entity"></property> 
  <property name="Name" column="Name"></property>   
  <property name="Ownership_Percent" access="property" 
            column="Ownership_Percent" type="Decimal"></property> 
</class> 
</hibernate-mapping> 

Solution

  • Your model properties need to be virtual:

        public class SomeInfo 
        {
            public virtual long     Form_Id { get; set; }   
            public virtual string   Account_Id { get; set; } 
            public virtual char     Entity { get; set; }
            public virtual string   Name { get; set; }
            public virtual decimal  Ownership_Percent { get; set; } 
        }