C# 属性id是对象的一部分';s键信息,无法修改
当我试图用EF更新数据时,我得到了类似的错误 属性id是对象的密钥信息的一部分,不能删除 修改 Thısıs wı表格应用程序 你可以在这里看到我的更新方法 地区更新 ------存储库更新方法---------C# 属性id是对象的一部分';s键信息,无法修改,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,当我试图用EF更新数据时,我得到了类似的错误 属性id是对象的密钥信息的一部分,不能删除 修改 Thısıs wı表格应用程序 你可以在这里看到我的更新方法 地区更新 ------存储库更新方法--------- 您得到的错误消息是准确的——您正在设置_truck.Id属性,按照惯例,它是实体框架使用的主键/标识字段。您可能只想向数据库和数据模型(如果您首先使用EF代码)添加另一个字段,以保存txtplateNumber.Tag值。无论哪种方式,您都需要删除将该值设置为_truck.Id.的代码
您得到的错误消息是准确的——您正在设置_truck.Id属性,按照惯例,它是实体框架使用的主键/标识字段。您可能只想向数据库和数据模型(如果您首先使用EF代码)添加另一个字段,以保存txtplateNumber.Tag值。无论哪种方式,您都需要删除将该值设置为_truck.Id.的代码。我以前做过,我再次尝试了您的答案,但结果相同error@Emre不确定您是否更新了上面的代码,但看起来您仍在尝试将值设置在顶部附近,您正在设置_truckmodel的初始值:_truck.Id=Convert.ToInt32(txtplateNumber.Tag);这将引发您所说的异常…没关系,我更改了代码,但这次我添加了两个不同的项目,当我尝试选择第一个项目并更改一些值并更新它时,第一个和第二个项目发生相同的情况。第一个项目Id为空。。。
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();
}