简体   繁体   English

NHibernate:映射IList属性

[英]NHibernate: Mapping IList property

I have next tables Order , Transaction , Payment . 我有下表, OrderTransactionPayment 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? 如何在数据库中保留TransactionsPayments清单(关系)?

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. 这使得父实体上的操作可以应用于集合实体。

  • It does not usually make sense to enable cascade on a <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. Cascade通常用于<one-to-one><one-to-many>关联。
  • If the child object's lifespan is bounded by the lifespan of the parent object, make it a life cycle object by specifying cascade="all-delete-orphan". 如果子对象的生命周期受父对象的生命周期限制,请通过指定cascade =“all-delete-orphan”将其设置为生命周期对象。
  • Otherwise, you might not need cascade at all. 否则,您可能根本不需要级联。 But if you think that you will often be working with the parent and children together in the same transaction, and you want to save yourself some typing, consider using cascade="persist,merge,save-update" 但是如果你认为你经常在同一个交易中与父母和孩子一起工作,并且你想节省一些打字,可以考虑使用cascade =“persist,merge,save-update”

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: 假设TransactionsPayments已经在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM