c# – 属性id是对象的密钥信息的一部分,不能修改
发布时间:2020-12-15 22:14:07 所属栏目:百科 来源:网络整理
导读:当我尝试用EF更新数据时,我得到了错误 the property id is part of the object’s key information and cannot be modified 这是一种应用程序 你可以在这里看到我的更新方法 区域更新 try { _truck.plateNumber= txtplateNumber.Text; _truck.brand = txtMar
当我尝试用EF更新数据时,我得到了错误
这是一种应用程序 区域更新 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.Information); } } 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(); } 解决方法
您获得的错误消息是准确的 – 您正在设置_truck.Id属性,按照惯例,该属性是Entity Framework使用的主键/标识字段.您可能只想在数据库中添加另一个字段,如果使用EF Code First,则可以添加到数据模型中,以保存txtplateNumber.Tag值.无论哪种方式,您将需要删除该值设置为_truck.Id的代码.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 学习FlexViewer(三)——继承自BaseWidget
- Postgresql自动填充?
- c# – WPF UIElement.IsHitTestVisible = false;仍然返回命
- C++防止头文件被重复引入的3种方法(详解版)
- Cocos2dx.3x入门三部曲-Hello Game项目解析(三)
- Swift CAShapeLayer,UIBezierPath绘制图形
- fastjson json高性能处理类
- 教程:深入理解Flash的沙箱 – Application Domains
- Flex 与Servlet交互(通过request传递值)
- 将cocos2dx项目从Visual Studio 迁移到 xcode