两个(几乎)相同的C#/实体框架按钮 - 单击处理程序表现不同

时间:2018-06-01 23:55:16

标签: c# entity-framework entity-framework-6

这只是我的第二篇文章,请原谅任何协议错误。

我有两个Button-Click处理程序几乎是复制&粘贴图像,但行为差异导致问题(和挫折)

两者都是将新的子记录添加到员工数据库(Entity Framework 6, Code First),一个是新的电话号码,另一个是职位。

电话号码:

private void btn_NewRecord_Click(object sender, EventArgs e)
{
    if (thisNumber != null)
    {
        DbEntityEntry entry = _fDBC.Entry(thisNumber);

        if (entry.State == EntityState.Modified)
        {
               //  PROMPT to Save or Cancel current modified record
        }
    }

    EmployeePhoneNumber newNumber = new EmployeePhoneNumber();
    newNumber.EmployeeID = thisEmployee.EmployeeID;
    int highestKey = _fDBC.EmployeePhoneNumbers          //  This is the only difference between these two Handlers,  
                                                         //  Phone Numbers use a non-automated multi-part Key
                          .Where(a => a.EmployeeID == thisEmployee.EmployeeID)
                          .Select(a => a.EmployeePhoneNumberID)
                          .DefaultIfEmpty(-1)                       // Default if set IS EMPTY
                          .Max();

    newNumber.EmployeePhoneNumberID = highestKey + 1;

    try
    {
        _fDBC.EmployeePhoneNumbers.Add(newNumber);

        // INITIALIZING the Validation Checklist here
    }
    catch (DuplicateKeyException ex)
    {
         //  Not getting any exceptions
    }

    bs_EmployeePhoneNumbers.MoveLast();       //  This works fine for the new PhoneNumber
                                              //  The binding source sees the new record and moves to it,
                                              //  the new record is displayed on the Form.
    thisNumber = newNumber;
}

此代码在上下文中添加一个新的电话号码,移动到它,并在返回时,新的(空白)记录显示在准备填写的表单上。

现在很奇怪

位置:

private void btn_NewPosition_Click(object sender, EventArgs e)
{
    if (thisPosition != null)
    {
        DbEntityEntry entry = _fDBC.Entry(thisPosition);

        if (entry.State == EntityState.Modified)
        {
                 // PROMPT to save or cancel current, modified record.
        }
    }

    EmployeePosition newPos = new EmployeePosition();
    newPos.EmployeeID = thisEmployee.EmployeeID;               
    newPos.StartDate = DateTime.Today;

    try
    {
        _fDBC.EmployeePositions.Add(newPos);

        // INITIALIZING the Validation checklist here

    }
    catch (DuplicateKeyException ex)
    {
           //  Not getting any exceptions
    }

    bs_EmployeePos.MoveLast();       //  This Code does NOT work. 
                                     //  The bindingsource does NOT see the new record, the move has no effect, 
                                     //  and the current (existing) record is displayed on the form.
    thisPosition = newPos;

}

此代码确实创建了一条新记录,可以在调试器中看到。 Bindingsource只是看不到它。

问题是'为什么一个有效,另一个没有?'

同样,这些处理程序之间的唯一区别是电话号码处理程序中的LINQ查询,用于查找新记录的子索引值。该查询(显然)在ADD指令之前发生。

我可以通过在移动之前重新定义Position Bindingsource的DataSource来解决这个问题:

bs_EmployeePos.DataSource = thisEmployee.EmployeePositions.Where(ep => ep.EndDate == null).ToList();

但是这会导致其他问题 - 它会阻止我的[取消]按钮处理程序成功运行。

有什么想法吗?

感谢。 罗斯

增加:EmployeePhoneNumber&的定义EmployeePosition:

public partial class EmployeePosition
{
    public int EmployeePositionID { get; set; }

    public int PositionID { get; set; }

    public int EmployeeID { get; set; }

    [Column(TypeName = "date")]
    public DateTime StartDate { get; set; }

    public int PayrollClassID { get; set; }

    public int? InterviewerID { get; set; }

    public int? AssignedWorkstationID { get; set; }

    [Column(TypeName = "date")]
    public DateTime? EndDate { get; set; }

    public decimal FTE { get; set; }

    [Column(TypeName = "tinyint")]
    public Byte? EmployeeRCD { get; set; }                  //  ADD 

    public virtual WorkStation WorkStation { get; set; }

    public virtual Employee Employee { get; set; }

    public virtual PayrollClass PayrollClass { get; set; }

    public virtual Position Position { get; set; }
}


public partial class EmployeePhoneNumber
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EmployeeID { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int EmployeePhoneNumberID { get; set; }

    public int PhoneNumberTypeID { get; set; }

    [Required]
    [StringLength(3)]
    public string AreaCode { get; set; }

    [Required]
    [StringLength(8)]
    public string PhoneNumber { get; set; }

    [StringLength(7)]
    public string PhoneExtention { get; set; }

    [StringLength(25)]
    public string PhoneNumberNote { get; set; }

    public virtual Employee Employee { get; set; }

    public virtual LU_PhoneNumberTypes LU_PhoneNumberTypes { get; set; }
}

0 个答案:

没有答案