Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate - Map 2 tables to one class

I have a table structure something like this

table Employees
 EmployeeID
 EmployeeLogin
 EmployeeCustID

table Customers
 CustomerID
 CustomerName

What i would like is to map the structure above to one single class named:

Class Employee
 EmployeeID
 EmployeeLogin
 EmployeeName

How do i do that with fluent nhibernate ?

like image 248
Morten Schmidt Avatar asked May 19 '09 06:05

Morten Schmidt


2 Answers

I don't know if it is possible with fluent, but in xml you use the join element:

simplified:

<class name="Employee" table="Customers" >
  <id name="CustomerID" .../>

  <property name="CustomerName"/>

  <join table="Employees">
    <key column="EmployeeCustID" />
    <property name="EmployeeLogin" />
  </join>

</class>

See this post by Ayende

like image 84
Stefan Steinegger Avatar answered Oct 29 '22 14:10

Stefan Steinegger


I agree with Frans above but if you're stuck with someone else's code and have to use the existing structure, you can can use WithTable.

public class EmployeesMap : ClassMap<Employees>
{
    public EmployeesMap()
    {
        Id(x => x.EmployeeId);
        Map(x => x.EmployeeLogin);

        WithTable("Customers", join =>
            {
                join.Map(m => m.EmployeeName, "CustomerName");
                join.WithKeyColumn("EmployeeCustID");
            });
    }
}

[DataContract(IsReference = true)]
public class Employees
{
    [DataMember]
    public virtual int EmployeeId { get; set; }

    [DataMember]
    public virtual string EmployeeLogin { get; set; }

    [DataMember]
    public virtual string EmployeeName { get; set; }
}
like image 21
Marius Smit Avatar answered Oct 29 '22 16:10

Marius Smit