views:

614

answers:

1

I have a Fluent Nhibernate map like :

 public class UserMap : ClassMap<PortalUser>
{
    public UserMap()
    {
        WithTable("aspnet_Users");
        Id(x => x.Id, "UserId")
            .GeneratedBy.Guid();
        Map(x => x.Name, "UserName");
        Map(x => x.Login, "LoweredUserName");
        WithTable("LdapUsers", m => m.Map(x => x.FullName, "FullName"));

    }
}

My foreign key column in table "LdapUser" is UserId but the select that gets generated is going to look for a "PortalUserId".
Is there a way to specify the relation key direcly?

+2  A: 

Try this:

...
WithTable("LdapUsers", m => {
    m.Map(x => x.FullName, "FullName");
    m.WithKeyColumn("UserId");
});
Stuart Childs
I think you mean m.WithKeyColumn("UserId");
Jamie Ide
WithTable is creating a <join> element and, if I understand correctly, <key> is supposed to refer the the key column on the joined table. Ronnie said his foreign key is "LdapUser" so the query should look something like ... FROM aspnet_Users a INNER JOIN LdapUsers b ON a.UserId = b.LdapUser.My reference: http://ayende.com/Blog/archive/2007/04/24/Multi-Table-Entities-in-NHibernate.aspxBut you could be right, Jamie; I honestly have no idea... I've never had to use this construct. :)
Stuart Childs
Actually my foreign column was "UserId" in table "LdapUser" and putting it instead of "LdapUser" worked perfectly. Thanks again.
Ronnie
Glad it worked... and sorry Jamie, you were dead on but I wasn't paying enough attention apparently. I updated my post in case someone stumbles upon this later.
Stuart Childs
Hey Stuart this is ok for setting the key of the linked table but it automatically joins with the primary key of the base table. How to setup a join with a different column that is not the pk?
Ronnie
I'm not sure that this is possible to be honest. You might want to create a new question for that and link back to this one for full context. You're likely to get more exposure for your new question that way... maybe someone else knows a way to do it.
Stuart Childs