实体框架代码优先:两次引用相同的实体



我有两个类,如下

联系人类别

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

LeaveTake类别

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。

我该如何建立这种关系?我觉得Leave Taken类中有两个FK引用了同一个Contact类,导致了这种情况的发生。

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

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

试试这个。

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

最新更新