NHibernate.MappingException: NHibernate.PropertyNotFoundException: 在 class 中找不到 属性 的 getter

NHibernate.MappingException: NHibernate.PropertyNotFoundException: Could not find a getter for property in class

nHibernate 将我的枚举映射到数据库时遇到问题。

尝试查看映射但没有发现任何错误,我怀疑这可能与枚举有关?

这里是Fortron.Crm.Domain.Policy:

namespace Fortron.Crm.Domain
{
   [Serializable]
   public class Policy
   {
      private PolicyStatus _policyStatus;

      public PolicyStatus PolicyStatus
      {
          get { return _policyStatus; }
          set { _policyStatus = value; }
      }
   }

这是 class 映射

  <class name="Fortron.Crm.Domain.Policy, Fortron.Crm.Domain" table="Policy" lazy="false">
    <id name="Id" access="nosetter.camelcase-underscore" column="PolicyId" unsaved-value="-1">
      <generator class="identity" />
    </id>
    <set name="Claims" access="field.camelcase-underscore" inverse="true" lazy="false" cascade="save-update">
      <key column="PolicyId" />
      <one-to-many class="Fortron.Crm.Domain.Claim, Fortron.Crm.Domain" />
    </set>
    <many-to-one name="Product" column="ProductId" access="nosetter.camelcase-underscore" />
    <property name="PolicyNumber" />
    <property name="VehicleRegistrationNumber" />
    <property name="ContractNumber" />
    <property name="ContractPaymentAuthorised" />
    <property name="ContractPaymentAuthorisedAt" />
    <component name="Contact" access="nosetter.camelcase-underscore">
      <property name="Title" />
      <property name="GivenNames" />
      <property name="Surname" />
      <property name="BusinessName" />
      <property name="DateOfBirth" />
      <property name="Gender" column="GenderId" />
      <property name="TelephoneNumber" />
      <property name="MobileTelephoneNumber" />
      <property name="WorkTelephoneNumber" />
      <component name="Address" access="nosetter.camelcase-underscore">
        <property name="StreetLine1" column="StreetLine1" />
        <property name="StreetLine2" column="StreetLine2" />
        <property name="CityTown" column="CityTown" />
        <property name="Postcode" column="Postcode" />
        <many-to-one name="StateTerritory" column="StateTerritoryId" />
      </component>
    </component>
    <property name="CustomerNumber" column="CustomerNumber" not-null="false" />
    <property name="Vin" column="Vin" not-null="false"  />
    <property name="PolicyStatus" column="PolicyStatusId" />
  </class>

最后是堆栈跟踪:

Service cannot be started. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.MappingException: Could not compile the mapping document: Fortron.Crm.Domain.Policy.hbm.xml ---> NHibernate.MappingException: Problem trying to set property type by reflection ---> NHibernate.MappingException: class Fortron.Crm.Domain.Policy, Fortron.Crm.Domain, Version=2.0.0.1, Culture=neutral, PublicKeyToken=6f168f2566a816b4 not found while looking for property: PolicyStatus ---> NHibernate.PropertyNotFoundException: Could not find a getter for property 'PolicyStatus' in class 'Fortron.Crm.Domain.Policy'
   at NHibernate.Properties.BasicPropertyAccessor.GetGetter(Type type, String propertyName)
   at NHibernate.Util.ReflectHelper.ReflectedPropertyClass(String className, String name, String accessorName)
   --- End of inner exception stack trace ---
   at NHibernate.Util.ReflectHelper.ReflectedPropertyClass(String className, String name, String accessorName)
   at NHibernate.Mapping....

有什么想法吗?

更改您的会员签名使其成为virtual:

public virtual PolicyStatus PolicyStatus

NHibernate 期望您的实体的所有成员都是 virtual 绑定。这是必要的,因为 NHibernate 在某些情况下会创建代理(以实现延迟加载)。没有标记为 virtual,这对于 NHibernate 是不可能的。

可以参考this (or archive)文章:

The quick answer to that question is: because we need members to be virtual in order to do our lazy loading magic/voodoo.

The longer answer is more interesting though. An important feature that any real ORM must have is transparent Lazy Loading. If you retrieve an object through an ORM, you don't want it to automatically pull in an entire object graph (not by default anyway), yet you don't want to litter your code with checks to see if certain associations have been loaded yet, and then loading them if necessary. This is the ORM's responsibility. Ideally, you want to be able to access properties and have the ORM load the necessary data upon first access of those properties if the data hasn't been retrieved yet.

NHibernate has this ability, yet it doesn't require you to inherit from some kind of NHibernate base class or implement any interfaces or anything like that. So how does it work? Well, NHibernate uses proxies of your classes at runtime whenever lazy loading is required. Ok, so what exactly is a proxy? In this case, an NHibernate proxy is a type which is generated dynamically when NHibernate is initialized for your application (this only happens once upon application startup). A proxy type will be generated for each of your entities that hasn't explicitly been mapped to avoid lazy loading (more on this later). A proxy type for one of your entities will actually inherit from your entity, and will then intercept each possible call you can perform on that type.