【发布时间】:2015-01-02 03:34:01
【问题描述】:
我有一个大问题!在 nhibernate 中映射具有多个联合子类的抽象类时,我不断收到异常。 当我只使用一个联合子类时,我没有收到任何错误并且一切正常。当我使用更多子类时,会发生此错误:
[TargetException: 对象与目标类型不匹配。] System.Reflection.RuntimeMethodInfo.CheckConsistency(对象目标)+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 文化) +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(对象目标)+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(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) +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 方法,我不知道如何解决它?
【问题讨论】:
-
异常似乎很明显。您具体遇到了什么问题?如果你真的需要帮助,你应该发布一个更好的代码示例。见stackoverflow.com/help/mcve
-
感谢您的回复!对不起,我已经编辑了这个问题。我不明白我应该如何做得更好。我希望你这样做?
标签: c# asp.net nhibernate nhibernate-mapping union-subclass