开发者

NHibernate: How to map a column on a referenced table to a primitive in my object?

If I have the following existing table schema

+-------------------+        +-------------------+
|  Address          |        |  Country          |
+-------------------+        +-------------------+
|  Line1        开发者_如何学Python    |   +--->|  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?


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);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜