我有一个包含两个表的数据库,ProductLine和Supplier。生产线 包含外键SupplierId,而Supplier包含字段“Name”。
表ProductLine:ProductLineId(PK),名称,SupplierId(FK)等。
表供应商:SupplierId(PK),名称等。
在我的计划中,我有两个类,ProductLine和Supplier,其中ProductLine的成员为“Supplier SupplierId”。此成员在ProductLine.hbm.xml中按以下方式映射 (我对ProductLine的NHibernate-mapping):
<many-to-one name="SupplierId" class="Supplier"/>
在我的网站上,我想要一个gridview来显示所有产品线的名称和 相关供应商。出于这个原因,我构建了一个方法GetAllProductLines()和 将它绑定到gridview。此外,我将以下数据字段附加到gridview:
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="SupplierId" HeaderText="Supplier" />
gridview显示:
姓名:xy(正确)
供应商:[名称空间]。供应商
出了什么问题?我怎样才能得到供应商的名字? 如果这还不够,请询问更详细的信息。
MG,J。Carl
答案 0 :(得分:0)
如果您正在使用
<many-to-one name="SupplierId" class="Supplier"/>
您希望拥有供应商ID - 而不是实体。如果您有一个实体而不是您需要使用
Supplier.SupplierId
字段
答案 1 :(得分:0)
好的,我可以通过一个很好的例子帮助解决问题。
在Supplier类中,我需要覆盖以下ToString() - 属性 方式:
public override string ToString()
{
return this.Name;
}
通过这种方式,GetAllProductLines-Method将我交给了供应商的名称。