我有一个带有多个表的继承类结构。我的班级结构如下。(这不是真正的课程。我在这里创建了模仿原件的假课程以节省空间。)
class Account {
public virtual int AID { get; set; }
public virtual string Name { get; set; }
public virtual string Serial { get; set; }
public virtual string Type { get; set; }
public Account { Type = "Default"; }
}
class Human : Account {
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
// more properties..
public Human { Type = "Human"; }
}
class Bank : Account {
public virtual string BranchName { get; set; }
// more properties..
public Bank { Type = "Bank"; }
}
我的 Account.hbm.xml 文件如下所示。
<class name="Account">
<id name="AID" type="Int32">
<generator class="native" />
</id>
<discriminator column="Type" type="string" />
<property name="Name" />
<property name="Serial" />
<property name="Type" />
<subclass name="Human" discriminator-value="Human">
<join table="Account">
<key column="AID" />
<property name="FirstName" />
<property name="LastName" />
</join>
</subclass>
<subclass name="Bank" discriminator-value="Bank">
<join table="Account">
<key column="AID" />
<property name="LocationName" />
</join>
</subclass>
</class>
我执行此操作,并使用正确的字段集创建表。问题是当我插入记录时。
我按以下方式进行。
Bank bank = new Bank();
bank.Name = "Any Name";
bank.Serial = "0001";
bank.BranchName = "Local CIty";
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(bank);
transaction.Commit();
}
}
下面是 NUnit 产生的错误。也许我做错了什么。有人可以帮我吗?
NHibernate.PropertyValueException : Error dehydrating property value for
xxxx.xxxxx.Bank.Name
----> System.IndexOutOfRangeException : Parameter index is out of range.