The property identifier is part of the object key information and cannot be changed

When I try to update data using EF, I got an error like

the property identifier is part of the object's key information and cannot be modified

Thıs ıs wınForm app you can see my update method here

renewal area

        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

      

------ Method for updating repository ---------

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();
    }

      

+3


source to share


1 answer


The error message you are getting is accurate - you are setting the _truck.Id property, which by convention is the primary key / ID field used by the Entity Framework. You can just add another field to your database and to your data model if you are using EF Code First to store the txtplateNumber.Tag value. In any case, you will need to remove the code setting whose value is _truck.Id.



0


source







All Articles