2014-10-20 171 views
0

複合鍵映射不適用於以下情況。流利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;組; } 公共虛擬字符串名稱{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

找到解決方案寧可說找到了我正在做的錯誤。 Equal和GetHashCode方法留在我的代碼中實現。 下面是校正entiity,

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