【问题标题】:Update one record using entity frame work使用实体框架更新一条记录
【发布时间】:2020-01-22 13:05:45
【问题描述】:

我想使用实体框架将一条记录更新到数据库。当更新此员工的所有日期时,设置 Null Only Employee Code And IsActive .. 如何仅更新 isAcive

这是我的代码

       private void btn_Save_Resignation_Click(object sender, EventArgs e)
       {
          try
           {
                   var IsActive = new database.tblEmployeeData
                   {

                       EmployeeCode = Convert.ToInt32(txtEmpCode.Text),
                       IsActive = cbxResignationEmp.Checked = true, 
                   };


                       db.tblEmployeeDatas.AddOrUpdate(IsActive);
                       db.SaveChanges();
                       MessageBox.Show("تم إقالة الموظف بنجاح", "Sigma Software", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       ClearResignation();

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Sigma Software", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
}

....... 这是我的模型课

   public partial class tblEmployeeData
   {
       [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
       public tblEmployeeData()
       {
           this.tblBlackListForEmps = new HashSet<tblBlackListForEmp>();
           this.tblContractForEmploees = new HashSet<tblContractForEmploee>();
           this.tblCustodyForEmps = new HashSet<tblCustodyForEmp>();
           this.tblDocumentEmployeeWithStates = new HashSet<tblDocumentEmployeeWithState>();
           this.tblOtherDataForEmps = new HashSet<tblOtherDataForEmp>();
           this.tblPenaltyForEmployees = new HashSet<tblPenaltyForEmployee>();
       }

       public int EmployeeCode { get; set; }
       public string EmployeeName { get; set; }
       public Nullable<byte> GenderCode { get; set; }
       public Nullable<byte> PranchCode { get; set; }
       public Nullable<byte> RelationShipCode { get; set; }
       public Nullable<byte> AdministrationCode { get; set; }
       public Nullable<byte> DepartmentCode { get; set; }
       public Nullable<short> JopCode { get; set; }
       public Nullable<byte> JopLevelCode { get; set; }
       public Nullable<byte> ConCustmerCode { get; set; }
       public Nullable<byte> NationalityCode { get; set; }
       public Nullable<byte> TypeOfWorkersCode { get; set; }
       public Nullable<bool> IsActive { get; set; }

【问题讨论】:

  • 如果要更新记录,应该使用数据库中已有的记录,而不是插入新记录。
  • 首先找到您的记录,然后更改它,并将其设置为已修改然后保存更改 -HTH ;).

标签: c# entity-framework insert-update


【解决方案1】:

正如here解释的那样:

实体框架不会神奇地找出哪些属性有 改变与哪些属性没有改变,它需要实体, 如果存在,将实体按原样输入数据库 目前人满为患。如果实体不存在,则将其插入 数据库。

所以我建议你用下面的方式改变你的代码:

var empCode = Convert.ToInt32(txtEmpCode.Text);
var IsActive = db.tblEmployeeDatas.FirstOrDefault(e => e.EmployeeCode == empCode);
if (IsActive == null)
{
   IsActive = new database.tblEmployeeData
   {
      EmployeeCode = empCode,
   };
}
IsActive.IsActive = cbxResignationEmp.Checked = true, // BTW should it be assigning or condition?

db.tblEmployeeDatas.AddOrUpdate(IsActive);
db.SaveChanges();
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2014-11-16
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多