0

我有 2 类如下

接触类

public class Contact : Person
{
    public Contact() { }

    //public string WebPageAddress { get; set; }
    public string NickName { get; set; }
    public string SpouseName { get; set; }
    public DateTime? Datejoin { get; set; }

    public double NoOfLeave { get; set; }
    double _totalTake;
    public double TotalTake
    {
        get
        {
            double total = 0;
            if (LeaveTakenDetails != null)
            {
                if (LeaveTakenDetails.Count() > 0)
                {
                    foreach (LeaveTakenDetails leave in LeaveTakenDetails)
                    {
                        total += leave.TotalDayTake;
                    }
                }
            }

            if (total == 0)
            {
                return _totalTake;
            }
            else
            {             
                if(total > _totalTake)
                {
                    return total;
                }
                else
                {
                    return _totalTake;
                }

            }
            //return total;
        }
        set { _totalTake = value; }
    }
    public double RemainLeave { get; set; }

    public TitleOfCourtesy TitleofCourtesy { get; set; }
    public DateTime? Anniversary { get; set; }
    [FieldSize(4096)]
    public string Notes { get; set; }
    public virtual Position Position { get; set; }
    public virtual IList<DemoTask> TrackedTasks { get; set; }
    public virtual Department Department { get; set; }

    [DataSourceProperty("Department.Contacts", DataSourcePropertyIsNullMode.SelectAll)]
    [DataSourceCriteria("Position.Title = 'Manager'")]
    public virtual Contact Manager { get; set; }


    public virtual IList<LeaveTakenDetails> LeaveTakenDetails { get; set; }
}
public enum TitleOfCourtesy { Dr, Miss, Mr, Mrs, Ms };

请假上课

public class LeaveTakenDetails : INotifyPropertyChanged 
{
    public LeaveTakenDetails() { }

    [Key]
    [Browsable(false)]
    public Int32 LeaveID { get; protected set; }

    private double totaltake;
    [Editable(false)]
    [Appearance("LeaveTake", Enabled=false)]
    [ImmediatePostData]
    public double TotalDayTake
    {
        get
        {

        }
        set
        {

        }
    }

    public DateTime Datefrom {
        get;    set;

    }

    public DateTime DateTo
    {
        get;

        set;    
    }
    public virtual LeaveType LeaveType { get; set; }
    public bool AMSession { get; set; }
    public virtual Department Department { get; set; }
    public string Reason { get; set; }
     public DateTime? ApproveDate { get; set; }

    public virtual Contact ApproveBy { get; set; }
    public bool Halfday { get; set; }

    public int Employee_ID { get; set; }
    [ForeignKey("Employee_ID")]
    public virtual Contact Employee { get; set; }

}

我的 ApproveBy 和 Employee 属性是指同一个 Contact 类。当我运行代码时,代码首先会为我生成表格。当我运行程序并尝试添加休假记录并选择员工 A 时,批准人是员工 C 并保存记录。我去员工表格寻找员工A,但是没有员工A的休假记录,但是添加的休假记录是属于员工A的。

我怎样才能为此建立关系?我觉得休假类中有两个 FK 指的是同一个联系人类并导致这种情况发生。

知道我可以在员工表格中看到休假记录属于员工 A 吗?请帮忙!谢谢你。

4

1 回答 1

0

您在一个表中有两个外键指向同一个表,一个用于 Employee,一个用于 Approved by,因此您需要在另一端(联系人)有两个集合。

像这样试试。

public class Contact : Person
{
  ...
  ...
  public virtual IList<LeaveTakenDetails> LeaveApprovalDetails { get; set; }
  public virtual IList<LeaveTakenDetails> LeaveContactDetails { get; set; }
  ...
  ...
}

public class LeaveTakenDetails : INotifyPropertyChanged 
{
  ..
  ..
  public int ApproveBy_ID { get; set; }
  public virtual Contact ApproveBy { get; set; }
  public int Employee_ID { get; set; }
  public virtual Contact Employee { get; set; }
  ..
  ..
}

并在模型创建中指定关系。

public class Context : DbContext
{
    ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       modelBuilder.Entity<LeaveTakenDetails>()
          .HasRequired(m => m.ApproveBy)
          .WithMany(t => t.LeaveApprovalDetails)
          .HasForeignKey(m => m.ApproveBy_Id)
          .WillCascadeOnDelete(false);

       modelBuilder.Entity<Contact>()
          .HasRequired(m => m.Employee)
          .WithMany(t => t.LeaveContactDetails)
          .HasForeignKey(m => m.Employee_Id)
          .WillCascadeOnDelete(false);
    }
}
于 2015-03-20T04:56:34.390 回答