views:

19

answers:

1

If I have the following existing table schema

+-------------------+        +-------------------+
|  Address          |        |  Country          |
+-------------------+        +-------------------+
|  Line1            |   +--->|  CountryId        |
|  Line2            |   |    |  Name             |
|  City             |   |    +-------------------+
|  State            |   |
|  Zip              |   |
|  CountryId        |---+
+-------------------+

...and my class is as follows

public class Address
{
    public virtual string Line1 { get; set; }
    public virtual string Line2 { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string PostalCode { get; set; }
    public virtual string Country { get; set; }
}

...how do I configure my mappings so that the Country (string) property contains the [Name] column from the [Country] table?

+2  A: 

You can achieve this by mapping a view. But the object oriented approach would be to create a Country object and map the many-to-one relationship between Address and Country:

public class Address
{
    public virtual string Line1 { get; set; }
    public virtual string Line2 { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string PostalCode { get; set; }
    public virtual Country Country { get; set; }
}

You would then access the name through Address.Country.Name. In FluentNHibernate you would use References to map the relationship in your Address mapping: References(x=> x.Country, CountryId);

Jamie Ide
Thanks, but I understand this clearly. I would still like to know how to achieve the above result.
Jon Erickson
Your above result or mine? The only way to achieve yours is by mapping a view as I stated.
Jamie Ide
I read this question early on and came up with the exact same answer as Jamie. I didn't post in case I was wrong about this, but in my experience with NHibernate, I have not seen any other way to create the mapping you're looking for without using a view. It's like Domain object normalization! Maybe you can map a private _country property and then create a public Country property that returns a string by doing _country.Name? Similar idea for setting.
Chris F
Jamie and Chris are right.
James Gregory
Jon, I will reply with the Fluent NHibernate ClassMaps for this if that's what you;re looking for. Let me know.
Jamie Ide