我有一个简单的每个子类的表继承,具有以下 NHibernate 映射
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default- cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="BillingDetail table="BillingDetails">
<id name="Id" type="System.Int32">
<column name="Id" />
<generator class="identity" />
</id>
<property name="DateAdded" type="System.DateTime">
<column name="DateAdded" />
</property>
<many-to-one class="Account name="Account">
<column name="Account_id" />
</many-to-one>
<joined-subclass name="BankAccount table="BillingDetails_BankAccount">
<key>
<column name="Id"/>
</key>
<property name="AccountNumber" type="System.Int64">
<column name="AccountNumber" />
</property>
<property name="SortCode" type="System.Int32">
<column name="SortCode" />
</property>
</joined-subclass>
<joined-subclass name="CreditCard table="BillingDetails_CreditCard">
<key>
<column name="Id" />
</key>
<property name="CardNumber" type="System.Int64">
<column name="CardNumber" />
</property>
<property name="CardType" type="System.String">
<column name="CardType" />
</property>
<property name="ExpiryDate" type="System.DateTime">
<column name="ExpiryDate" />
</property>
</joined-subclass>
</class>
</hibernate-mapping>
所以信用卡和银行账户都继承自“账单明细”。在我的域层中,我有以下声明:
var billingDetail = (from a in unitOfWork.Context.BillingDetail.OfType<CreditCard>()
select a).FirstOrDefault();
上面的“unitOfWork.Context”属性只是让我可以访问 ISession 工作单元。当我运行应用程序时,我收到以下错误:
BillingDetail = 'unitOfWork.Context.BillingDetail' 抛出类型为 'NHibernate.PropertyAccessException' 的异常消息 =“无效的演员表(检查您的映射是否有属性类型不匹配);BankAccount 的设置器”
如果我在“var billingDetail...”语句中放置一个断点并运行程序来检查这个错误,我可以看到上面的消息。但是,如果我然后单击播放继续程序执行,而不是用上面的消息轰炸,程序运行成功并将所有数据输入数据库。如果不检查断点,程序就会崩溃(正如我所期望的,如果映射确实存在问题)。
似乎正在发生的事情是,每次看到“BankAccount”实体时都会出现异常,但所有 CreditCard 实体都很好。“OfType”是否有问题,它没有过滤掉 BankAccount 对象?