ASP.NET 需要 Fluent NHibernate Mapping 帮助
ASP.NET and Fluent NHibernate Mapping help needed
我正在尝试使用 ASP.NET MVC 和 Fluent NHibernate 在详细信息列表(管理页面)页面中显示外键列 'NAME' 字段值。
仅供参考:主要 Table 是 Employee
。它有一个外键(DeptId
)。
请看下面:
//This is my Employee Class
public class Employee : Entity<int>
{
public virtual string EmpName { get; set; }
public virtual int DeptId { get; set; }
public virtual string Address { get; set; }
public virtual DateTime DOB { get; set; }
public virtual Boolean Status { get; set; }
public virtual Department Department { get; set; }
}
//This is my Employee Map
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Table("Employee");
Id(e => e.Id).Column("EmpId");
Map(e => e.EmpName);
Map(e => e.DeptId);
Map(e => e.Address);
Map(e => e.DOB);
Map(e => e.Status);
}
}
<!--This is the view-->
@foreach (var emp in Model)
{
<tr>
<td>@Html.DisplayFor(e => emp.Id)</td>
<td>@Html.DisplayFor(e => emp.EmpName)</td>
<td>@Html.DisplayFor(e => emp.Department.Name)</td>
<td>@Html.DisplayFor(e => emp.Address)</td>
<td>@Html.DisplayFor(e => emp.DOB)</td>
<td>@Html.DisplayFor(e => emp.Status)</td>
<td>@Html.ActionLink("Edit","UpdateEmployee", new {empId = emp.Id}) | @Html.ActionLink("Delete","DeleteEmployee")</td>
</tr>
}
如何显示部门名称?
在您的 EmployeMap 中,您需要添加对外键的引用。不只是一张地图。
References(x => x.Department)
.TheColumnNameIs("ID")
.PropertyRef(d => d.DeptId);
由于您的两个表之间存在关系,您可以使用以下方法访问 Department 的所有属性:emp.Department.XXX
不确定你的 deptname 属性是什么
<td>@Html.DisplayFor(e => emp.Department.Deptname)</td>
查看您的实体和地图。你不告诉 NHibernate 关于员工和部门之间的引用。
您可以从 Entity class 中删除 public virtual int DeptId { get; set; }
并从 EmployeeMap 中删除 Map(e => e.DeptId);
。相反,您必须添加引用,如 eddie_31003 post:
References(x => x.Department, "DeptId");
我正在尝试使用 ASP.NET MVC 和 Fluent NHibernate 在详细信息列表(管理页面)页面中显示外键列 'NAME' 字段值。
仅供参考:主要 Table 是 Employee
。它有一个外键(DeptId
)。
请看下面:
//This is my Employee Class
public class Employee : Entity<int>
{
public virtual string EmpName { get; set; }
public virtual int DeptId { get; set; }
public virtual string Address { get; set; }
public virtual DateTime DOB { get; set; }
public virtual Boolean Status { get; set; }
public virtual Department Department { get; set; }
}
//This is my Employee Map
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Table("Employee");
Id(e => e.Id).Column("EmpId");
Map(e => e.EmpName);
Map(e => e.DeptId);
Map(e => e.Address);
Map(e => e.DOB);
Map(e => e.Status);
}
}
<!--This is the view-->
@foreach (var emp in Model)
{
<tr>
<td>@Html.DisplayFor(e => emp.Id)</td>
<td>@Html.DisplayFor(e => emp.EmpName)</td>
<td>@Html.DisplayFor(e => emp.Department.Name)</td>
<td>@Html.DisplayFor(e => emp.Address)</td>
<td>@Html.DisplayFor(e => emp.DOB)</td>
<td>@Html.DisplayFor(e => emp.Status)</td>
<td>@Html.ActionLink("Edit","UpdateEmployee", new {empId = emp.Id}) | @Html.ActionLink("Delete","DeleteEmployee")</td>
</tr>
}
如何显示部门名称?
在您的 EmployeMap 中,您需要添加对外键的引用。不只是一张地图。
References(x => x.Department)
.TheColumnNameIs("ID")
.PropertyRef(d => d.DeptId);
由于您的两个表之间存在关系,您可以使用以下方法访问 Department 的所有属性:emp.Department.XXX
不确定你的 deptname 属性是什么
<td>@Html.DisplayFor(e => emp.Department.Deptname)</td>
查看您的实体和地图。你不告诉 NHibernate 关于员工和部门之间的引用。
您可以从 Entity class 中删除 public virtual int DeptId { get; set; }
并从 EmployeeMap 中删除 Map(e => e.DeptId);
。相反,您必须添加引用,如 eddie_31003 post:
References(x => x.Department, "DeptId");