PinkCoder
PinkCoder

Reputation: 19

Using Fluent NHibernate, how to map one entity to differently named tables?

To be clear, I am looking to map one entire object and all its properties to different copies of basically the same table. My searches show me how to split an object's properties across multiple tables, but that is not what I am trying to accomplish.

Here is my object model (stripped down):

class Customer 
{
   public Guid CustomerGuid { get; set; }
   public string Name { get; set; }
   public Address Address { get; set; }
}

class Address
{
   public Guid AddressGuid { get; set; }
   public string Line1 { get; set; }
   public string State { get; set; }
}

class Application
{
   public Guid ApplicationGuid { get; set; }
   public Address Address { get; set; }
   public DateTime SubmittedDate { get; set; }
}

The problem is that I need the Address to act sort like a component, but be saved into two separate tables: CustomerAddress and ApplicationAddress, as such:

table Customer
(
   CustomerGuid
   Name
)

table Application
(
   ApplicationGuid
   SubmittedDate
)

table CustomerAddress
(
   CustomerGuid
   Line1
   State
)

table ApplicationAddress
(
   ApplicationGuid
   Line1
   State
)

I know I can accomplish one of the mappings using as one-to-one (HasOne) for say Customer to CustomerAddress, but then how can I do the same thing with Application to ApplicationAddress?

Upvotes: 1

Views: 1629

Answers (1)

Firo
Firo

Reputation: 30803

for customer and analog for Application

class CustomerMap : ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x => x.Id, "CustomerGuid").GeneratedBy.GuidComb();

        Map(x => x.Name);
        Join("CustomerAddress", join =>
        {
            join.KeyColumn("CustomerGuid");
            join.Component(x => x.Address, c =>
            {
                c.Map(x => x.Line1);
                c.Map(x => x.State);
            });
        });
    }
}

Upvotes: 1

Related Questions