需要ASP.NET和Fluent NHibernate Mapping帮助

时间:2015-04-18 15:57:27

标签: c# asp.net-mvc fluent-nhibernate

我试图通过使用ASP.NET MVC和Fluent NHibernate在详细列表(管理页面)页面中显示外键列'NAME'字段值。

仅供参考:主表为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>
    }

如何显示部门名称?

3 个答案:

答案 0 :(得分:0)

在EmployeMap中,您需要添加对外键的引用。不只是地图。

References(x => x.Department)
    .TheColumnNameIs("ID")
    .PropertyRef(d => d.DeptId);

答案 1 :(得分:0)

由于您的两个表之间存在关联,因此您可以使用以下方式访问Department的所有属性:emp.Department.XXX
不确定你的deptname attrbute的名字是什么

<td>@Html.DisplayFor(e => emp.Department.Deptname)</td>

答案 2 :(得分:0)

查看您的实体和地图。你没有告诉NHibernate有关Employee和Department之间的参考资料。 您可以从Entity类中删除public virtual int DeptId { get; set; },从EmployeeMap中删除Map(e => e.DeptId);。相反,您必须添加引用,如eddie_31003 post:

References(x => x.Department, "DeptId");
相关问题