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

c# entity-framework entity-framework-6

Question

I received an error message when attempting to update a data set using EF.

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

This wnForm application allows you to view my update procedure here.

area Update

        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 Repository Update ————-

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();
    }
1
4
5/10/2015 5:00:38 PM

Accepted Answer

ZZZ_tmp
1
5/8/2015 8:19:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow