I’m trying to map a couple of legacy tables using Entity Framework. The classes look like this…
public class Customer
{
[Key, Required]
public string Code { get; set; }
public string Domain { get; set; }
public virtual Address BillToAddress { get; set; }
public virtual ICollection<Address> ShipToAddresses { get; set; }
}
public class Address
{
[Column(Order = 0), Key, Required]
public string Code { get; set; }
[Column(Order = 1), Key, Required]
public string Domain { get; set; }
public string Type { get; set; }
public string CustomerReferenceCode { get; set; }
}
Each Customer has one “BillToAddress” that corresponds with an Address whose CustomerReferenceCode contains the Customer’s Code and where the Type field contains the text “Customer“
Each Customer has zero or more “ShipToAddresses” that correspond to Addresses whose CustomerReferenceCode contains the Customer’s Code and whose where the Type fields contain the text “Ship-To“
I’m able to reference the BillToAddress by adding
[Key, Required]
[ForeignKey("BillToAddress"), Column(Order = 1)]
public string Code { get; set; }
[ForeignKey("BillToAddress"), Column(Order = 2)]
public string Domain { get; set; }
But I’ve not been able to figure out how to reference the collection of ShipToAddresses for the customer.
See this example (note it is separate classes): Fluent NHibernate automap inheritance with subclass relationship
One easy approach might be:
One additional comment – this is not implicitly forcing your one Billing address business logic as the example you start with does, so you either need to enforce that elsewhere with the above approach. Also, creating two classes and using TPH as described in the example I link to is the approach I would likely take anyway – which would meet your described goal above directly. Also, in between the two, this may work, using the discriminator in the getter of each property directly.