I have to 2 entities like this:
class A
{
int id { get; set; }
string Name { get; set; }
}
class B
{
int id { get; set; }
A RefToA { get; set; }
string Name { get; set; }
}
How can I map this 2 classes so that i would have 3 tables like this:
-
table A with 2 columns: id and name
-
table B with 2 columns: id and name
-
table AB with 2 columns: AId and BId
If I understand this correct you are creating a ref table because you want the reference to be nullable. If that is the case, you do not need a ref table. Simply set the FK in table b as nullable. Then you can map it a simple reference. Then you would have tables like this:
Aid (nullable)
And you can map it like this:
Update
There is no way of mapping this how you want in nhibernate (and no other orm for that matter). The reason for this is quite simple: it violates quite a few rules and there is never a reason to do it this way. The correct way to do this is to have a nullable fk reference in table b. That is how you represent a reference in a sql database. It is simply bad design to use many-to-many when you mean one-to-many and it will most certainly give you trouble later on.