Entity Framework 代码优先:两次引用相同的实体

Entity Framework Code First: Refer to same entities twice

我有 2 class 如下

联系Class

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 };

请假Class

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 属性指的是同一个联系人 class。当我 运行 代码时,代码首先会为我生成 table。当我运行程序并尝试添加请假记录和select员工A时,批准人是员工C并保存记录。我去Employee form找A员工,没有A员工的请假记录,但是添加的请假记录是A员工的。

我怎样才能建立这种关系?我觉得是 Leave Taken class 中有两个 FK 指的是同一个 Contact class 并导致这种情况发生。

我能在员工表单中看到属于员工 A 的休假记录吗? 请帮忙! 谢谢。

您将两个外键合二为一 table 指向同一个 table 一个用于 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);
    }
}