C# 使用实体框架更新一条记录
我想使用实体框架将一条记录更新到数据库。更新此记录时,此员工的所有日期仅设置空员工代码和IsActive。。如何只更新isAcive 这是我的密码C# 使用实体框架更新一条记录,c#,entity-framework,insert-update,C#,Entity Framework,Insert Update,我想使用实体框架将一条记录更新到数据库。更新此记录时,此员工的所有日期仅设置空员工代码和IsActive。。如何只更新isAcive 这是我的密码 private void btn_Save_Resignation_Click(object sender, EventArgs e) { try { var IsActive = new database.tblEmployeeData
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; }
公共部分类tblEmployeeData
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共tblEmployeeData()
{
this.tblBlackListForEmps=new HashSet();
this.tblContractForEmploees=new HashSet();
this.tblCustodyForEmps=new HashSet();
this.tblDocumentEmployeeWithStates=new HashSet();
this.tblOtherDataForEmps=newhashset();
this.tblPenaltyForEmployees=new HashSet();
}
public int EmployeeCode{get;set;}
公共字符串EmployeeName{get;set;}
公共可为空的性别代码{get;set;}
公共可为空的PranchCode{get;set;}
公共可为空的关系代码{get;set;}
公共可为空的管理代码{get;set;}
公共可为空的部门代码{get;set;}
公共可空JopCode{get;set;}
公共可为空的JopLevelCode{get;set;}
公共可为空的Nancestmercode{get;set;}
公共可空的国有代码{get;set;}
公共可为空的TypeOfWorkersCode{get;set;}
公共可空IsActive{get;set;}
如前所述:
实体框架无法神奇地找出哪些属性具有
已更改与未更改的属性相比,它采用实体,
如果该实体存在,则将其按原样输入数据库
当前已填充。如果实体不存在,则将其插入
数据库
因此,我建议您以下一种方式更改代码:
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();
...
如果要更新记录,应使用数据库中现有的记录,而不是插入新记录。首先查找记录,然后对其进行更改,并将其设置为“已修改”,然后保存更改-HTH;)。另请查看: