使用 fluent nhibernate 时,我是否做错了多对多? [英] Am I doing many to many incorrectly when using fluent nhibernate?

查看:17
本文介绍了使用 fluent nhibernate 时,我是否做错了多对多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个主要实体(数据库表)

I have two main entities (db tables)

  1. 项目
  2. 申请

我有一个名为 ProjectApplication 的桥,它有 3 个列(Id、ProjectId、ApplicationId)

I have a bridge tabled called ProjectApplication with 3 col (Id, ProjectId, ApplicationId)

一个项目可以有多个应用程序.一个应用程序可以在许多不同的项目下

A project can have many applications. An application can below to many different project

您的基本多对多映射

目前这是我在流畅的 nhibernate 映射文件中设置的内容

Currently this is what i have setup in my fluent nhibernate mapping files

 public class ProjectMap
 {
        HasMany(x => x.ProjectApplications)
      .AsBag().Inverse().Cascade.AllDeleteOrphan().Fetch.Select().BatchSize(80);
 }

 public class ApplicationMap
 {
      HasMany(x => x.ProjectsApplications)
          .AsBag().Inverse().Fetch.Select().BatchSize(50);

 }

这是否有任何缺点,因为我看到有一个 HasManyToMany 语法,所以我不确定它是否对生成的查询或性能等产生影响

Is there any downside to this as i see there is a HasManyToMany syntax so I am not sure if it makes a difference in terms of the Queries that are generated or performance, etc

请指教

推荐答案

通常有两种方法,正如您正确提到的:

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

  • 配对对象的显式映射,导致one-to-manymany-to-one
  • 使用 many-to-many
  • 在不了解基础表的情况下进行隐式映射
  • 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:

要在这里添加更多内容,我首先要提到的是,使用 many-to-many 我们正在从模型中丢失配对对象.永远.因此,一旦我们的客户来问:请建立我的关系主要,或介绍排序 - 我们根本做不到.关系是这样的.没办法怎么扩展.

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.

其次,也是最有可能的 - 很可能:我们的客户会来问:您能否为我创建一个过滤器,只选择与 Projects 相关的 Projectscode>Application 将某些设置设为 true AND ...

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 ...

这在 many-to-many 的情况下会有点挑战.

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

有一些Subquery力量的例子:

好吧,这就是我的观点.不是说是对的.但我的经验表明,通过显式对对象映射,我们已经为扩展和复杂查询做好了准备.

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.

这篇关于使用 fluent nhibernate 时,我是否做错了多对多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆