Search code examples
asp.net-mvc-4nhibernatefluent-nhibernatenhibernate-mappingfluent-nhibernate-mapping

NHibernate fluent mapping to an entity property


I have a table called "Customers" and in this table there are store and office address. In the code there is a Customer class with two properties that are of type Address (one for StoreAddress and OfficeAddress).

public class Customer
{
    public virtual int Id { get; set;}
    public virtual string Name {get;set;}
    public virtual string Email {get; set;}
    public virtual Address StoreAddress {get; set;}
    public virtual Address OfficeAddress {get; set;}
}

public class Address
{
    public string Address1 {get; set;}
    public string Address2 {get; set;}
    public string State {get; set;}
    public string City {get; set;}
    public string Zip  {get; set;}
}

I can map items that are not of an entity type Address but not sure how to map to another entity property within the customer entity?..

Table("Customers");
Schema("dbo);
Id(x => x.ID).Column("CustomerId");
Map(x => x.Name);
Map(x => x.Email);

How would I be able to map to my StoreAddress and OfficeAddress from the table Customers table?


Solution

  • You can use component mapping:

    Component(x => StoreAddress).ColumnPrefix("StoreAddress");
    Component(x => OfficeAddress).ColumnPrefix("OfficeAddress");
    

    Then create a component map for Address type:

    public class AddressMap : ComponentMap<Address>
    {
        public AddressMap()
        {
            //map properterties
        }
    }