NHibernate:如何将引用表上的列映射到对象中的基元?

时间:2010-02-03 17:02:27

标签: nhibernate fluent-nhibernate

如果我有以下现有表格架构

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

......我的班级如下

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

...如何配置我的映射,以便Country (string)属性包含[Name]表中的[Country]列?

1 个答案:

答案 0 :(得分:2)

您可以通过映射视图来实现此目的。但是面向对象的方法是创建一个Country对象并映射Address和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; }
}

然后,您将通过Address.Country.Name访问该名称。在FluentNHibernate中,您可以使用References映射地址映射中的关系: References(x=> x.Country, CountryId);