Am I doing many to many incorrectly when using fluent nhibernate?

后端 未结 1 1046
忘掉有多难
忘掉有多难 2020-12-07 05:27

I have two main entities (db tables)

  1. Project
  2. Application

I have a bridge tabled called Projec

1条回答
  •  一生所求
    2020-12-07 05:52

    In general there are two approaches, as you've correctly mentioned:

    • explicit mapping of the pairing object, resulting in one-to-many and many-to-one
    • implicit mapping without any knowledge of the underlying table using many-to-many

    I (my personal statement) would avoid many-to-many in almost any scenario (while in some very rare, really admin object scenario could be used).

    Here are some of my tries, to explain that:

    • How to create NHibernate HasManyToMany relation
    • many-to-many with extra columns nhibernate
    • Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?

    To add more here, I would firstly mention, that with many-to-many we are loosing the pairing object from the model. Forever. So, once our customer will come and ask: please, make one of my relations Main, or introduce the Sorting - we simply cannot. The relation is as it is. No way how to extend it.

    And secondly, and most likely - very likely: our customer will come and ask: Could you create a filter for me, selecting only Projects which are related to Application having some setting set to true AND ...

    And that would be a bit challenging in many-to-many case.

    The scenario with explicit pairing object brings more overhead with that third Entity. But could be converted into Subqueries

    There are some examples of the Subquery power:

    • Query on HasMany reference
    • Is it possible to query all objects that have one or more possible children using NHibernate?

    Well, that is my point of view. Not saying it is correct. But my experience shows, that with explicit pair object mapping we are ready for extensions as well as for complex queries.

    0 讨论(0)
提交回复
热议问题