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]
表中的 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过映射视图来实现此目的。但面向对象的方法是创建一个 Country 对象并映射 Address 和 Country 之间的多对一关系:
然后您将通过 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:
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);