我有一个现有的 POCO 类库,其中子集合都存储在数组中。例如,Customer 类有一个 Invoice[] 数组来保存其发票:
class Customer
{
public int ID;
public Invoice[] _invoices;
}
class Invoice
{
public int ID;
public int CustomerID;
public string SomeData;
}
我无法更改这些类,我想使用 NHibernate 将它们映射到现有数据库。
我查看了 <array>
映射,但它似乎需要 <index>
元素。在我的数据库中,[Invoice]
表没有索引列。加载客户的发票时,我不希望它们位于数组中的任何特定位置。
我有哪些选择?
最佳答案
不幸的是,你的选择是改变你的类(class)或你的 table 。
A) 您可以更改 Customer
将发票申报为 IList
而不是数组;然后您将能够将它们映射为 unsorted bag .您甚至可以按某些列对其进行排序:
<bag name="Invoices" table="Invoices" order-by="ID ASC"> <!-- order-by is optional -->
<key column="CustomerID"/>
<element column="SomeData" type="String"/>
<!-- or use one-to-many if Invoice is mapped as entity -->
<one-to-many class="Whatever.Invoice, Whatever"/>
</bag>
B).您可以更改您的表以包含一个索引列并将您的发票映射为真实 <array>
:
<array name="Invoices" table="Invoices" order-by="ID ASC"> <!-- order-by is optional -->
<key column="CustomerID"/>
<index column="IndexNr"/>
<element column="SomeData" type="String"/>
</array>
关于nhibernate - 如果表中没有索引,如何配置 NHibernate 映射到数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1746414/