对象与目标类型不匹配。异常发生的getter

时间:2014-11-05 19:45:09

标签: c# asp.net nhibernate nhibernate-mapping union-subclass

我有一个很大的问题!在nhibernate中映射带有多个union-subclasses的抽象类时,我一直遇到异常。 当我只使用一个union-subclass时,我没有得到任何错误,一切正常。当我使用更多子类时,会发生此错误:

  

[TargetException:Object与目标类型不匹配。]          System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)+10909543          System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)+115          System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)+54          System.Reflection.RuntimePropertyInfo.GetValue(Object obj,BindingFlags invokeAttr,Binder binder,Object [] index,CultureInfo culture)+61          System.Reflection.RuntimePropertyInfo.GetValue(Object obj,Object [] index)+19          NHibernate.Properties.BasicGetter.Get(对象目标)+99

     

[PropertyAccessException:发生异常,发生了MedControlWeb.Models.Logs.Log.Code的getter]          NHibernate.Properties.BasicGetter.Get(Object target)+183          NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue,IGetter identifierGetter,IType identifierType,ConstructorInfo构造函数)+160          NHibernate.Tuple.PropertyFactory.BuildIdentifierProperty(PersistentClass mappedEntity,IIdentifierGenerator generator)+200          NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass,ISessionFactoryImplementor sessionFactory)+775          NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass,ICacheConcurrencyStrategy cache,ISessionFactoryImplementor factory)+835          NHibernate.Persister.Entity.UnionSubclassEntityPersister..ctor(PersistentClass persistentClass,ICacheConcurrencyStrategy cache,ISessionFactoryImplementor factory,IMapping mapping)+220          NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass模型,ICacheConcurrencyStrategy缓存,ISessionFactoryImplementor工厂,IMapping cfg)+369          NHibernate.Impl.SessionFactoryImpl..ctor(配置cfg,IMapping映射,设置设置,EventListeners侦听器)+ 2199          NHibernate.Cfg.Configuration.BuildSessionFactory()+ 181

我的hbm文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MedControlWeb"
                   namespace="MedControlWeb.Models.Logs">

  <class name="Log" abstract="true" lazy="false">
    <id name="Code" column="code">
      <generator class="hilo"/>
    </id>

    <property name="Description" column="description"/>
    <property name="User" column="user"/>
    <property name="Timestamp" column="timestamp"/>
    <property name="Action" column="action"/>

    <union-subclass name="SettingsLog" table="settings_log" lazy="false">
    </union-subclass>

    <union-subclass name="JobLog" table="job_log" lazy="false">
    </union-subclass>

  </class>
</hibernate-mapping>

我的抽象超级课程:

public class Log
{
    public int Code { get; set; }
    public DateTime Timestamp { get; set; }
    public MedControlWeb.Enums.Action Action { get; set; }
    public string Description { get; set; }
    public int User { get; set; }
}

其中一个子类:

public class SettingsLog : Log
{        
}

我哪里错了?

编辑: 我该怎么做才能解决这个错误?我没有看到我如何修复它,因为我有适当的get方法?

1 个答案:

答案 0 :(得分:0)

不管你信不信,这里的解决方案非常简单。这是映射:

<class name="Log" ...
    ...
    <union-subclass name="SettingsLog" table="settings_log" lazy="false">
    </union-subclass>

    <union-subclass name="JobLog" table="job_log" lazy="false">
    </union-subclass>

这是 class SettingsLog的片段:

public class SettingsLog : Log // this is it, SettingsLog is also a Log
{...}

这是有效的,SettingsLog - 也是Log。报告的问题不是来自事实:

  

...使用多个 union-subclasses映射抽象类时获得异常......

但事实上,另一个映射的union-subclass不是Log

public class JobLog // this is NOT a Log..
{...}

这就是为什么我们可以看到:

  

例外:对象与目标类型不匹配
   (好吧 - 是的,因为JobLog不是Log

所以,只要确保JobLogLog的子类......一切都将开始工作

  

<强> public class JobLog : Log