流畅的Nhibernate复合键映射错误

时间:2014-10-20 14:15:46

标签: fluent-nhibernate

复合键映射在以下场景中不起作用。

数据库表如下。

   Employee { Emp_ID, Name, Role_ID } (Role_ID is foreign key from Role table);
   Leave    { Leave_ID, Leave_Date, Leave_Comment}; 
   Employee_Leave  { Emp_ID, Leave_ID, Approval }; (EMP_ID and Leave_ID are composite key from Employee and Leave table respectively)

实体类如下。     班级员工     {         公共虚拟字符串ID {get;组; }         public virtual string Name {get;组; }         public virtual Role EmpRole {get;组; }     }

public class Leave
{
    virtual public Int16 LeaveID { get; set; }
    virtual public String LeaveDate { get; set; }
    virtual public String Comment { get; set; }
}

 public class EmployeeLeaveApproval
 {
    public virtual string EMP_ID { get; set; }
    public virtual int Leave_ID { get; set; }
    public virtual string Approval { get; set; }
 }

映射类如下。

 public class EmployeeMap : ClassMap<Employee>
 {
     public EmployeeMap()
     {
         Table("Employee");
         Id(x => x.ID, "ID");
         Map(x => x.Name, "NAME");
         References(x => x.EMPRole, "ROLE_ID").Not.LazyLoad();
      }
  }

   public class LeaveMap : ClassMap<Leave>
   {
       public LeaveMap()
       {
           Table("Leave");
           Id(x => x.LeaveID, "LEAVE_ID");
           Map(x => x.LeaveDate, "LEAVE_DATE");
           Map(x => x.Comment, "LEAVE_COMMENT");
       }
    }

下面的类映射工作正常。

    public class EmployeeLeaveApprovalMap : ClassMap<EmployeeLeaveApproval>
    {
       public EmployeeLeaveApprovalMap()
       {
           Table("Employee_Leave");
           Id(x => x.EMP_ID, "EMP_ID");
           Map(x => x.Leave_ID, "LEAVE_ID");
           Map(x => x.Approval, "Approval");
        }
     }

下面的类映射不起作用。

    public class EmployeeLeaveApprovalMap : ClassMap<EmployeeLeaveApproval>
    {
       public EmployeeLeaveApprovalMap()
       {
           Table("Employee_Leave");
           CompositeId()
             .KeyProperty(x => x.EMP_ID, "EMP_ID")
             .KeyProperty(x => x.Leave_ID, "LEAVE_ID");
           Map(x => x.Approval, "Approval");
        }
     }

获取错误“未通过数据库方法配置数据库。”同时调用方法BuildSessionFactory。

许多人提前感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

找到解决方案而不是说发现了我正在做的错误。 在我的代码中实现了Equal和GetHashCode方法。 以下是更正的实体,

 public class EmployeeLeaveApproval : Object
{
    public virtual string EMP_ID { get; set; }
    public virtual int Leave_ID { get; set; }
    public virtual string Approval { get; set; }

    public EmployeeLeaveApproval() {}

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;

        EmployeeLeaveApproval EL = (EmployeeLeaveApproval)obj;
        if (EL == null)
            return false;

        if (EMP_ID == EL.EMP_ID && Leave_ID == EL.Leave_ID)
            return true;

        return false;
    }

    public override int GetHashCode()
    {
        return (EMP_ID + "|" + Leave_ID).GetHashCode();
    }
}

regards..Dharmendra