如何使用 FluentNHibernate 映射 NHibernate 自定义集合?

发布于 2024-09-08 03:54:32 字数 969 浏览 4 评论 0原文

我试图绘制两天的集合图但没有成功。我还阅读了所有可能的文章和论坛,但仍然在那里。好的,问题来了:

1)集合类包含一个私有字段“_internalCollection”,它与 NHib 映射。

2) 持有实体应通过只读属性公开集合。

3)我想避免实现NHibernate接口IUserCollectionType!!!

我用 xml 映射做到了这一点,效果很好。 WarehouseEntity 是一个集合 Item。 Warehouses 是 OrgEntity 类中的只读属性。

  <component name="Warehouses" class="Core.Domain.Collections.EntitySet`1[Core.Domain.OrgStructure.IWarehouseEntity,Core],Core">
    <set name="_internalCollection" table="`WAREHOUSE`" cascade="save-update" access="field" generic="true" lazy="true" >
      <key column="`WarehouseOrgId`" foreign-key="FK_OrgWarehouse" />
      <!--This is used to set the type of the collection items-->
      <one-to-many class="Domain.Model.OrgStructure.WarehouseEntity,Domain"/>
    </set>
  </component>

知道如何使用流畅的 NHibernate 来做到这一点吗?

编辑:Core.Domain.Collections.EntitySet`1 是基集合类。它提供了处理集合的基本功能,并且可以适合任何 IEntity 接口的类。

I am trying to map the collection for two days without success. I also read all possible articles and forums but still down there. Ok, here is the problem:

1) The collection class contains a private field "_internalCollection" which is mapped with NHib.

2) The holding entity should expose the collection trough readonly property.

3) I want to avoid implementing NHibernate interface IUserCollectionType!!!

I did this with xml mapping and it work great. The WarehouseEntity is a collection Item. Warehouses is a readonly property in OrgEntity class.

  <component name="Warehouses" class="Core.Domain.Collections.EntitySet`1[Core.Domain.OrgStructure.IWarehouseEntity,Core],Core">
    <set name="_internalCollection" table="`WAREHOUSE`" cascade="save-update" access="field" generic="true" lazy="true" >
      <key column="`WarehouseOrgId`" foreign-key="FK_OrgWarehouse" />
      <!--This is used to set the type of the collection items-->
      <one-to-many class="Domain.Model.OrgStructure.WarehouseEntity,Domain"/>
    </set>
  </component>

Any idea how can I do it with fluent NHibernate?

EDIT: Core.Domain.Collections.EntitySet`1 is base collection class. It provides basic functionality for working with collections and can fit any class which is IEntity interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2024-09-15 03:54:32

尝试:

HasMany(x => x.Warehouses)
    .AsSet().KeyColumn("WarehouseOrgId")
    .Access.CamelCaseField(Prefix.Underscore)
    .ForeignKeyConstraintName("FK_OrgWarehouse");

编辑:我错过了问题的关键部分,所以这是另一个尝试:

Component(x => x.Warehouses, m =>
    {
        m.HasMany<Warehouse>(Reveal.Member<EntitySet<IWarehouseEntity>>("_internalCollection")
           .AsSet().KeyColumn("WarehouseOrgId")
           .ForeignKeyConstraintName("FK_OrgWarehouse");
    });

我确信不是这样,但希望它能让您走上正确的道路。还可以使用 ComponentMap 看看。

我的建议是完全避免自定义集合。我用 IEnumerable 上的扩展方法替换了我们的所有方法。

Try:

HasMany(x => x.Warehouses)
    .AsSet().KeyColumn("WarehouseOrgId")
    .Access.CamelCaseField(Prefix.Underscore)
    .ForeignKeyConstraintName("FK_OrgWarehouse");

Edit: I missed a key part of the question so here's another try:

Component(x => x.Warehouses, m =>
    {
        m.HasMany<Warehouse>(Reveal.Member<EntitySet<IWarehouseEntity>>("_internalCollection")
           .AsSet().KeyColumn("WarehouseOrgId")
           .ForeignKeyConstraintName("FK_OrgWarehouse");
    });

I'm sure that's not it but hopefully it puts you on the right path. Also have a look using ComponentMap.

My advice is to avoid custom collections entirely. I replaced all of ours with extension methods on IEnumerable<T>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文