【问题标题】:the property id is part of the object's key information and cannot be modified属性 id 是对象的关键信息的一部分,不能修改
【发布时间】:2015-05-11 01:00:38
【问题描述】:

当我尝试使用 EF 更新数据时,我遇到了类似的错误

属性id是对象关键信息的一部分,不能 修改

这是 wınForm 应用程序 你可以在这里查看我的更新方法

地区更新

        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", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    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(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();
    }

【问题讨论】:

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


    【解决方案1】:

    您收到的错误消息是准确的 - 您正在设置 _truck.Id 属性,按照惯例,它是实体框架使用的主键/身份字段。如果您使用 EF Code First,您可能希望简单地将另一个字段添加到您的数据库和数据模型中,以保存 txtplateNumber.Tag 值。无论哪种方式,您都需要删除将该值设置为 _truck.Id 的代码。

    【讨论】:

    • 我之前做过,我再次尝试了你的答案,但它抛出了同样的错误
    • @Emre 不确定您是否更新了上面的代码,但看起来您仍然试图将值设置在顶部附近,您正在设置 _truck 模型的初始值:_truck.Id = Convert.ToInt32(txtplateNumber.Tag);这将抛出你正在谈论的异常......
    • 没关系我改变了我的代码但是这次我添加了 2 个不同的项目,当我尝试选择第一个项目并更改它的一些值并更新它。第一个和第二个项目相同。第一个项目 ID 为空...
    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 2015-10-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多