How to map m2m tables with properties in the joining table

7 views
Skip to first unread message

Michael W Powell

unread,
Oct 1, 2025, 6:02:14 PMOct 1
to Fluent NHibernate
Hello,

I want to map a joining table between A and B. I have a sense how to map the A and B sides of the relationship, one of which I think should be .Inverse(), yes?

I am not hundred percent positive, but I may need to attach additional properties than just linking A to B and vice versa.

So I need to introduce a map for the joining table C as well. But, personally, I'm not quite sure how that would work.

Any insights?

Best, thank you...

Michael W. Powell

Michael W Powell

unread,
Oct 22, 2025, 6:08:40 PM (13 days ago) Oct 22
to Fluent NHibernate
Getting some errors after mapping the joining table. It is for whatever reason looking for a column i.e. vehiclereg0_.vehicleregistration_id, vreg being object 'table'. The base model Id property should be this:

Id(x => x.Id)
    .Column(nameof(ModelBase.Id))
    .Not.Nullable()
    .GeneratedBy.GuidComb();

Not sure why the joining is trying to label it "vehicleregistration_id" that is definitely incorrect.

How to persuade NH? Fluent NH? How to map it correctly?

The only columns that should really be mapped otherwise are:

References(x => x.Owner)
    .Not.Nullable();

References(x => x.Vehicle)
    .Not.Nullable();

TBD what level of inverse relationship there should be.

Note, vehicle can only be registered to one player at a time, so perhaps M2M is overkill.

Thoughts?

Thank you...

Michael W Powell

unread,
Oct 22, 2025, 6:14:40 PM (13 days ago) Oct 22
to Fluent NHibernate
Also it's not the only columns which are misidentified, or that I have not trained the ORM how to identify, YET... Which is what I'd like to puzzle through if it is even possible, vehiclereg0_.Player_id.

Honestly I'm not even sure how this SQL is being formed, unless perhaps I have a relationship or reference inversion incorrect perhaps.

SELECT vehiclereg0_.Player_id as player4_5_9_, vehiclereg0_.VehicleRegistration_id as vehicleregistration5_5_9_, vehiclereg1_.Id as id1_5_0_, vehiclereg1_.PlayerId as playerid2_5_0_, vehiclereg1_.VehicleId as vehicleid3_5_0_, player2_.Id as id1_4_1_, player2_.ServerProfileId as serverprofileid2_4_1_ ...  left outer join public.efcore_wbe_serverprofile serverprof9_ on motorvehic6_.ServerProfileId=serverprof9_.Id WHERE vehiclereg0_.Player_id=?

The example SQL above, not even sure how the majority of all this is even necessary, seems like extraneous junk happening somehow, because I am mapping a joining table? Is there a better base mapping class for that, perhaps? I'm not sure...

Michael W Powell

unread,
Oct 22, 2025, 6:35:52 PM (13 days ago) Oct 22
to Fluent NHibernate
Perhaps I am not mapping it quite right either side of the relationship.

From the player:
HasManyToMany(x => x.VehicleRegistrations)
    .Cascade.AllDeleteOrphan()
    .Table(JoinNames([TableNames.efcore, TableNames.wbe, nameof(VehicleRegistration).ToLower()]));

From the vehicle:
HasManyToMany(x => x.VehicleRegistrations)
    .Cascade.AllDeleteOrphan()
    .Inverse()
    .Table(JoinNames([TableNames.efcore, TableNames.wbe, nameof(VehicleRegistration).ToLower()]));

Whereas this somewhat dated example:

https://belter.io/manytomany-fluentnhibernate not mentioning any joining object whatsoever, which is a bit interesting to me.

Question though, in my joining table, what happens if I want to identify additional bits in that part of the model? i.e. is not just "joining two tables, with two FK references" and so forth.

If I go by the example, it appears to me the map drills right through that would-be entity.

Anyone?

Thanks...

Michael W Powell

unread,
Oct 26, 2025, 9:43:38 PM (9 days ago) Oct 26
to Fluent NHibernate
Trying a slightly different tact, instead of M2M joining table, there is no reason for the Owner to be more than a column or property, since Vehicle can have only one Owner. However, in this approach, Vehicle effectively has one and a half parents. One parent a Not Nullable, MotorVehicleDivision. The other parent, a Nullable, Player Owner.


Open to suggestions how to persuade the relationship to behave properly. On the Owner side, I've tried Refresh Cascade, same sort of effect, does not work. Also None Cascade.

Reply all
Reply to author
Forward
0 new messages