Przemek
Przemek

Reputation: 53

Fluent NHibernate Join Table not on primary Key

I'm trying to join two tables from DB but I need to join them on specified columns (not on keys). Tables look like:

EmplTable: Id, EmplId, FirstName, LastName
LoginTable: Id, EmplLoginId, Login

Domain object Employee:

public class Employee
{
    public virtual int Id { get; set; }
    public virtual String FirstName { get; set; }
    public virtual String LastName { get; set; }
    public virtual String Login { get; set; }
}

My Mapping looks like:

public EmployeeMap()
{
    Table("EmplTable");
    Id(x => x.Id).Column("Id");
    Map(x => x.FirstName).Column("FirstName");
    Map(x => x.LastName).Column("LastName");
    Join("LoginTable", m =>
    {
        m.Fetch.Join();
        m.KeyColumn("EmplId");
        m.Map(t => t.Login).Column("Login");
    });
}

What I'm trying to do is join tables on EmplTable.EmplId=LoginTable.EmplLoginId How can I change Join column from Id to EmplId

Upvotes: 1

Views: 5348

Answers (1)

Cole W
Cole W

Reputation: 15293

You could map it as a many-to-one and map it like this:

public EmployeeMap() {
    Table("EmplTable");
    Id(x => x.Id).Column("Id");
    Map(x => x.FirstName).Column("FirstName");
    Map(x => x.LastName).Column("LastName");
    References(x => x.Login, "EmplId")
        .Fetch.Join(); 
}

You would then need to create a Login object with the properties in your LoginTable

Edit:

I don't think this is currently possible with NHibernate to do this and therefore not possible with Fluent NHibernate.

https://nhibernate.jira.com/browse/NH-1452
https://nhibernate.jira.com/browse/NH-3143

Upvotes: 1

Related Questions