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

c# entity-framework entity-framework-6

Question

When I trying to update a data with EF I got error its like

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

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

region 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

------Repository Update method---------

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

The error message you are getting is accurate--you are setting the _truck.Id property, which by convention, is a primary key/identity field used by Entity Framework. You may want to simply add another field to your database, and to your data model if you are using EF Code First, to hold the txtplateNumber.Tag value. Either way, you are going to need to remove the code setting that value to _truck.Id.

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