Reputation: 2510
I want to map both the foreign key as a POCO property and the navigation property. IE in the example here , the answer substitutes the foreign key
public virtual int SongArtistID { get; set; }
I want to keep that key in the mapping. I have tried so far:
Map(x => x.ParentID);
References(x => x.Parent).Column("ParentID");
Map(x => x.ParentID);
References(x => x.Parent, "ParentID");
and many other combinations using the fluent mapper but I can't get it working. The problem is that the References()
directive always creates an extra column.
Upvotes: 3
Views: 4548
Reputation: 2510
As it turns out, mapping the way I showed was perfectly fine. The problem with the extra field was created by the other side of the relationship. What I had was:
class ParentMap
HasMany(x => x.Children)
.Inverse()
.Cascade.All();
class ChildMap
Map(x => x.ParentID);
References(x => x.Parent).Column("ParentID");
I had to specify the column on the HasMany()
of the parent instead of actually on the Reference()
of the child, like so:
class ParentMap
HasMany(x => x.Children)
.KeyColumn("ParentID") // Problem without this!
.Inverse()
.Cascade.All();
Upvotes: 3
Reputation: 52753
You don't map the same relationship twice in NHibernate.
It's just:
References(x => x.Parent, "ParentID")
And then you use this to get the FK value:
var parentId = obj.Parent.Id;
Or this to assign it:
obj.Parent = session.Load<TheClassOfTheParent>(parentId);
And none of those statements will result in a db call.
Upvotes: 4