views:

495

answers:

1

When automapping a joined subclass in fluent nhibernate, I can't figure out how to give the joined subclass a primary key.

public class Address:Entity {
 public virtual string Address1 { get; set; }
 public virtual string Address2 { get; set; }
 public virtual string City { get; set; }
 public virtual string State { get; set; }
 public virtual string Zip { get; set; }
 public virtual string Phone { get; set; }
 public virtual string Fax { get; set; }
 public virtual IList<Location> Locations { get; set; }
}

public class Location:Address {
 public virtual Address BillingAddress { get; set; }
 public virtual string OfficeHours { get; set; }
 public virtual string PatientAgeRestrictions { get; set; }
 public virtual bool WheelchairAccess { get; set; }
 public virtual string ContactPerson { get; set; }
 public virtual string ContactEmail { get; set; }
 public virtual string ContactPhone { get; set; }
 public virtual string ContactFax { get; set; }
 public virtual string TaxId { get; set; }
}

I want Location to have it's own id "location_ id" with it's own sequence. Then I want that mapped to address through an address_id column.

Right now it's generating the location with "addressid" as the primary key, which isn't what I want. How do I change this with the automapping?

A: 

I'm not sure you have a joined-subclass relationship. That is, by definition a joined subclass has the same ID as its parent class. For example, you might have a Person entity stored in your database for generic "people" information like name/age/etc and then an Employee subclass entity which is stored in a different table and holds data like position, salary, and dates of employment. So an Employee is a subtype of Person and to get the full "Employee-Person" object, you must join the two tables on their primary keys (e.g. SELECT * FROM Employee INNER JOIN Person ON Employee.Employee_id = Person.Person_id).

Are you sure about your relational model here? Is Location truly a subtype of Address? Inferring a bit from your property names, it seems to me that this is not what you intend. It seems like you probably have a many-to-many between an Address and an Organization (that is, there may be many "organizations" at the same address and an "organization" may have many addresses), with a "contact person" for the organization at a specific address. In which case you should map "organization", "contact", and another entity that defines the relationship between Address and "organization".

Stuart Childs
Yeah, you're right, I had already changed my code away from this model. I just wanted it set up this way so I could bind to it on my form like it was an address. I have everything working, though it's not exactly the way I wanted. Thanks for the answer.
Aaron Smith