views:

128

answers:

2

I face a problem when I try to get value from primary table in my mapping file.

My tables:

CREATE TABLE Customer
(
    [CustomerId] INT PRIMARY KEY,
    [FullName] NVARCHAR(50) NOT NULL
)

CREATE TABLE CustomerOrder
(
    [CustomerOrderId] INT PRIMARY KEY,
    [CustomerId] INT,
    [TransactionDate] DATETIME
)

My classes:

public class CustomerOrder
{
    public class Id {get; set;}
    public class CustomerName {get; set;}
    public class TransactionDate {get; set;}
}
...

How can I get FullName value and map to CustomerName property in my CustomerOrder fluent interface mapping class?

+1  A: 

You wouldn't, really. A better design would be to have a Customer property, which is an instance of a Customer class.

public class CustomerOrder
{
  public int Id { get; set; }
  public Customer Customer { get; set; }
}

public class CustomerOrderMap : ClassMap<CustomerOrder>
{
  public CustomerOrderMap()
  {
    Id(x => x.Id);
    References(x => x.Customer);
  }
}

Which you can then use like so:

customerOrder.Customer.FullName

If you really want to have a CustomerName property, you could create a delegating property on CustomerOrder.

public string CustomerName
{
  get { return Customer.FullName; }
}
James Gregory
A: 

If you want to map a class to multiple tables using Fluent-NHibernate, you can use the "WithTable" method.

    public CustomerOrderMap()
    {
        Id(x => x.Id);
        Map(x => x.TransactionDate);

        WithTable("Customer", m =>
        {
            // Use .TheColumnNameIs since the property name is different from the column name
            m.Map(x => x.CustomerName)
                .TheColumnNameIs("FullName");
        });
    }
Erik Öjebo