[英]NHibernate: Mapping IList property
I have next tables Order
, Transaction
, Payment
. 我有下表,
Order
, Transaction
, Payment
。 Class Order
has some properties: 班级
Order
有一些属性:
public virtual Guid Id { get; set; }
public virtual DateTime Created { get; set; }
...
and I have added two more: 我又增加了两个:
public virtual IList<Transaction> Transactions { get; set; }
public virtual IList<Payment> Payments { get; set; }
How to keep Transactions
and Payments
lists (relations) in the database? 如何在数据库中保留
Transactions
和Payments
清单(关系)?
See collection mapping in the reference documentation. 请参阅参考文档中的集合映射 。 There are several ways to map an
IList
property. 有几种方法可以映射
IList
属性。
A bag is an unordered, unindexed collection which may contain the same element multiple times. bag是无序的,无索引的集合,可以多次包含相同的元素。
<bag name="Transactions" lazy="true" table="Transaction" >
<key column="OrderId"/>
<one-to-many class="Transaction"/>
</bag>
A set is an unordered, unindexed collection which contains unique elements. 集合是一个无序的,无索引的集合,包含唯一的元素。
<set name="Transactions" lazy="true" table="Transaction" >
<key column="OrderId"/>
<one-to-many class="Transaction"/>
</set>
A list is an ordered and indexed collection which may contain the same element multiple times. 列表是有序和索引的集合,可以多次包含相同的元素。
<list name="Transactions" lazy="true" table="Transaction" >
<key column="OrderId"/>
<index column="ordering"/>
<one-to-many class="Transaction"/>
</list>
NHibernate can return the unordered collections in sorted order using the sort attribute. NHibernate可以使用sort属性以排序顺序返回无序集合。
No discussion of collection mapping us complete without mentioning cascade . 没有提到级联,没有讨论收集映射我们完整。 This enables operations on the parent entity to apply to the collection entities.
这使得父实体上的操作可以应用于集合实体。
<many-to-one>
or <many-to-many>
association. <many-to-one>
或<many-to-many>
关联上启用级联通常没有意义。 Cascade is often useful for <one-to-one>
and <one-to-many>
associations. <one-to-one>
和<one-to-many>
关联。 If you control the database then one of the joys of using NHib is that it will generate the database for you, including the foreign keys you need to maintain your lists there. 如果您控制数据库,那么使用NHib的一个好处就是它将为您生成数据库,包括在那里维护列表所需的外键。 Use the code below to do so:
使用以下代码执行此操作:
try
{
var schema = new SchemaExport(cfg);
schema.Drop(false, true);
schema.Create(true, true);
}
catch (Exception ex)
{
Console.WriteLine(@"Schema Export Error Message: {0}", ex);
}
where cfg is your NHibernate Configuration object. cfg是你的NHibernate配置对象。
Let us know if that solves your problem, or there is a different question you are asking. 如果这样可以解决您的问题,或者您提出的问题不同,请告诉我们。
Berryl Berryl
I'm not sure what you're asking. 我不确定你在问什么。 Assuming that
Transactions
and Payments
are mapped in nhibernate already, you need to add the cascade option to you association mappings for Order
.. In fluent nhibernate, something like this: 假设
Transactions
和Payments
已经在nhibernate中映射,你需要为Order
关联映射添加级联选项。在流畅的nhibernate中,类似这样的事情:
References(x => x.Payments)
.Cascade.All
...
That will make sure that when you add transactions/payments to an order, they are persisted. 这将确保当您向订单添加交易/付款时,它们会被持久化。
edit 编辑
Regardless of whether or not you're using fluent nhibernate, cascading is the option that (I think) you want. 无论你是否使用流利的nhibernate,级联是(我认为)你想要的选项。 The nhibernate docs for mapping collections are here .
映射集合的nhibernate文档就在这里 。
Figured out the working solution for me. 找出适合我的工作解决方案。 In the mapping file I have added:
在我添加的映射文件中:
<bag name="Transactions" lazy="true" table="Transaction" >
<key column="OrderId"/>
<one-to-many class="Transaction"/>
</bag>
<bag name="Payments" lazy="true" table="Payment">
<key column="OrderId"/>
<one-to-many class="Payment"/>
</bag>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.