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

发布于 2024-08-20 03:19:45 字数 927 浏览 6 评论 0原文

如果我有以下现有表架构

+-------------------+        +-------------------+
|  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] 表中的 /code> 列?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秉烛思 2024-08-27 03:19:45

您可以通过映射视图来实现此目的。但面向对象的方法是创建一个 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 中,您可以使用 引用 来映射地址映射中的关系:
参考文献(x=> x.Country, CountryId);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文