当我尝试用EF更新数据时,我得到了错误
the property id is part of the object’s key @R_11_4045@ion and cannot be
modified
区域更新
try { _truck.plateNumber= txtplateNumber.Text; _truck.brand = txtMarka.Text; _truck.model = txtModel.Text; _truck.type = txtTipi.Text; _truck.registrationDate = dtregistrationDate.Value; _truck.examinationDate = dtexaminationDate.Value; _truck.Description = txtDescription.Text; _truck.driverName = txtdriverName.Text; _truck.weight= txtweight.Text; _truck.Id = Convert.ToInt32(txtplateNumber.Tag); _truck.userId = Tools.Tools.getUserId(); #region update currentItem = cr.getbyId(Convert.ToInt32(txtplateNumber.Tag.ToString())).plateNumber; if (currentItem != null) { if (!currentItem.Equals(txtplateNumber.Text)) { if (!cr.isPlatealreadyExist(txtplateNumber.Text)) { DialogResult result = MessageBox.Show("Are you sure want to update to Truck?","Update",MessageBoxButtons.YesNo,MessageBoxIcon.Question); if (result == DialogResult.Yes) { if (!string.IsNullOrEmpty(this.pbLicence.ImageLocation)) { _truck.licencePicture = Tools.Tools.convertToByteFfromImageF(pbLicence.Image); if (!cr.getbyId(Convert.ToInt32(txtplateNumber.Tag)).hasPicture) { _truck.hasPicture = true; cr.Update(_truck); } } cr.Update(_truck); MessageBox.Show("Successfuly"); } else { MessageBox.Show("The process was Cancel !","Canceled",MessageBoxButtons.OK,MessageBoxIcon.@R_11_4045@ion); } } else { MessageBox.Show("The plate number is already Exists.","Same Plate Number",MessageBoxIcon.Stop); } } else { DialogResult result = MessageBox.Show("Are you sure want to update to Truck?",MessageBoxIcon.Question); if (result == DialogResult.Yes) { if (!string.IsNullOrEmpty(this.pbLicence.ImageLocation)) { _truck.licencePicture = Tools.Tools.convertToByteFfromImageF(this.pbLicence.Image); if (!cr.getbyId(Convert.ToInt32(txtplateNumber.Tag)).hasPicture) { _truck.hasPicture = true; cr.Update(_truck); } } cr.Update(_truck); MessageBox.Show("SuccessFully"); } } #endregion } else { MessageBox.Show("You did not select an Item","Warning"); } } catch (Exception ex) { MessageBox.Show("Error:" + ex.Message,"Error"); } finally { getUpdatedList(); Tools.Tools.clearallFormControlsContent(pickTruckControls()); } #endregion
——存储库更新方法———
public int Update(Truck item) { Truck updated = db.Trucks.Where(x => x.Id == item.Id).FirstOrDefault(); db.Entry(updated).CurrentValues.SetValues(item); return db.SaveChanges(); }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。