Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c# - Am I doing many to many incorrectly when using fluent nhibernate?

I have two main entities (db tables)

  1. Project
  2. Application

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

Your basic many to many mapping

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);

 }

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

Please advise

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...