更新实体框架不起作用的记录
本文关键字:记录 不起作用 框架 实体 更新 | 更新日期: 2023-09-27 18:00:47
这是我的代码:
...
Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text);
updatedBox = getBoxInfo();
entities.SaveChanges();
private Domain.Box getBoxInfo()
{
Domain.Box retBox = new Domain.Box();
retBox.BoxID = TextBoxBoxID.Text;
retBox.LocationID = Convert.ToDecimal(TextBoxLocationID.Text);
retBox.Positions = Convert.ToByte(TextBoxPositions.Text);
retBox.DiseaseID = RadComboBoxDisease.SelectedValue;
retBox.SampleTypeID = RadComboBoxSampleType.SelectedValue;
retBox.TubeTypeId = RadComboBoxTubeTypeID.SelectedValue;
return retBox;
}
代码编译和执行良好,但数据库不会更改,也就是说,所有信息都与更新前完全相同。任何帮助都将不胜感激,谢谢!
如果你想插入一个新的Domain.Box对象,你应该这样做:
entities.Boxes.AddObject(getBoxInfo());
entities.SaveChanges();
没有必要创建updatedBox
对象,因为你只是在覆盖它。如果我理解你的需求,你想执行插入,而不是更新。
如果我不正确,并且您正在尝试更新updatedBox
对象的某些属性,那么只需传递对该对象的引用并更新其属性:
Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text);
getBoxInfo(ref updatedBox);
entities.SaveChanges();
private void getBoxInfo(ref Domain.Box retBox)
{
retBox.BoxID = TextBoxBoxID.Text;
...
}
entities.SaveChanges();
我想你想这样做:
Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text);
UpdateBoxInfo(updatedBox);
entities.SaveChanges();
private void UpdateBoxInfo(Domain.Box theBox)
{
theBox.BoxID = TextBoxBoxID.Text;
theBox.LocationID = Convert.ToDecimal(TextBoxLocationID.Text);
theBox.Positions = Convert.ToByte(TextBoxPositions.Text);
theBox.DiseaseID = RadComboBoxDisease.SelectedValue;
theBox.SampleTypeID = RadComboBoxSampleType.SelectedValue;
theBox.TubeTypeId = RadComboBoxTubeTypeID.SelectedValue;
}
我会这样尝试:
Domain.Box updatedBox = entities.Boxes.FirstOrDefault(TextBoxBoxID.Text);
getBoxInfo(updatedBox);
entities.SaveChanges();
private void getBoxInfo(Domain.Box retBox)
{
retBox.LocationID = Convert.ToDecimal(TextBoxLocationID.Text);
retBox.Positions = Convert.ToByte(TextBoxPositions.Text);
retBox.DiseaseID = RadComboBoxDisease.SelectedValue;
retBox.SampleTypeID = RadComboBoxSampleType.SelectedValue;
retBox.TubeTypeId = RadComboBoxTubeTypeID.SelectedValue;
}