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

时间:2015-03-20 04:29:49

标签: entity-framework devexpress code-first xaf

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

LeaveTaken类

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.

我如何才能建立这种关系?我觉得在休假Taken课程中有两个FK引用相同的Contact类并导致这种情况发生。

我知道我可以看到离职记录属于员工表格中的员工A吗? 请帮忙! 谢谢。

1 个答案:

答案 0 :(得分:0)

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

试试这样。

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