views:

950

answers:

5

Hi

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 ?

A: 

Is EmployeeCustID unique? If not, this is never going to work, as you then try to cram two different entity types into 1. Also, with your structure, how do you want to save an instance? -> the CustomerID isn't known, so you can't save such an entity.

IMHO it's better to simply keep Customer as a related entity to Employee, as (I assume) the EmployeeCustID is used to link a Customer entity to an Employee entity if the employee is also a customer, which means 'customer' is just a role for employee and therefore optional and changeable and thus should be a separate entity.

Frans Bouma
A: 

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

Stefan Steinegger
Hi, you are right you could do that join from the Customers Table, but i would like to do it the other way around.Using the Employees Table and join the Customers table in however that results in it being impossible to join on the correct id because the customers table doesnt have the foreign key.
Morten Schmidt
Why would you like to do it different? Does it matter? It is only this way in the mapping. You don't see this in the classes nor the tables. So what?
Stefan Steinegger
In Fluent you'd use WithTable("2ndTableName", m => { /* 2nd table mappings */);
James Gregory
A: 

I have not tried this since Fluent NHibernate went to 1.0 so my syntax may be incorrect. I'm pretty sure this will only work if Customer.CustomerId is a foreign key to Employee.

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

    Table("Customer", m =>
    {
      m.Map(x => x.EmployeeName, "CustomerName");
    });
  }
}
ddc0660
I just read another comment that you don't FK Customer to Employee so this would not likely work.
ddc0660
A: 

My scenario is, map two tables to exclude records from the mapping criteria. Is it possible to map two tables that way? For e.g., Table A records (produc1, product2) Table B records (produc1, product2, product3, product5)

My map should be such that when I select records, it should only return Product3 and Product5 *Table A is always a subset of TableB.

rajeshC
A: 

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; }
}
Marius Smit

related questions