简体   繁体   中英

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> 

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; } 
    }

I guess that if you'd provide full exception, we will see (down in the stack trace) message like this:

... Could not find a getter for property 'ID' in class 'Some.Models.SomeInfo' ...

because the mapping instructs NHibernate to map C# property ID

<id name="ID" column="Form_Id"> 
  <generator class="native"></generator> 
</id>

and there is no property ID . Also, there is second mapping of the same column Form_Id

<property name="Form_Id" column="Form_Id"></property> 

And this is not how it should be. We should map one column to one property. So, this mapping should be working

<class name="SomeInfo" table="Some_Table" lazy="false"> 
  <id name="Form_Id" column="Form_Id"> 
     <generator class="native"></generator> 
  </id>
  <property name="Account_Id" column="Account_Id"></property> 
  ...
</class> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM