NHibernate has a system called Mapping by code that gives the developer the possibility to map their database in code (as the name says). I am using NHibernate 3.3.1.
I have done this for two classes: “Gebruiker” and “Lijst”. They both have a list of eachother so this is a classic example of a many to many relation.
The mapping is as follows:
Gebruiker
Set(l => l.Lijsten, map =>
{
map.Table("LijstGebruiker");
map.Cascade(Cascade.All);
map.Inverse(true);
map.Key(k => k.Column("Gebruiker"));
}, map => map.ManyToMany(p => p.Column("Lijst")));
Lijst
Set(l => l.Gebruikers, map =>
{
map.Table("LijstGebruiker");
map.Cascade(Cascade.All);
map.Key(k => k.Column("Lijst"));
}, map => map.ManyToMany(p => p.Column("Gebruiker")));
As far as I know this should result in the table "LijstGebruiker" with the columns Gebruiker and Key.
Instead: NHibernate generates a table LijstGebruiker with three columns, one extra in the addition to the two expected ones: elt. Elt also refers to Lijst using a foreign key.
According to the websites I found on the internet about this this should not happen, yet it is. What am I doing wrong?
Apparently, this is some kind of strange behavior that happened when column name equals class name. The mapping generated by your code is:
Gebruiker
Lijst
So, there is no column in
many-to-manyelement. It could be a bug in NHibernate.If you rename columns to
Gebruiker_idandLijst_idthen everything works fine.Another solution is to specify column name using multiple column definition method:
Gebruiker
Lijst
The mapping generated by the code is: