所以我有一个这个模型http://i.imgur.com/tz8ZVPT.png,我想用一个程序创建一个更新,所以我首先创建更新,填写所有必要的项目,找到一个 MachineType,也许一个程序,但如果没有找到,则从 MachineType 中获取第一个程序。注意:所有 MachineType 至少有 1 个程序。
for (int i = 0; i < dgvInput.Rows.Count; i++)
{
DataGridViewRow row = dgvInput.Rows[i];
int machineTypeId = Convert.ToInt32(row.Cells[1].Value);
Update update = new Update();
update.MachineType.Add(Check.prombase.MachineTypes.First(mt => mt.MachineTypeId == machineTypeId));
foreach (Program program in database.Programs)
{
if (program.ProgramVersion.Split('v')[0] == row.Cells[5].Value.ToString())
{ //If a program is added here, it will save it
update.Program.Add(program);
break;
}
}
if (update.Program.Count == 0)
foreach (Program program in database.Programs)
{ //When a program is added here, it will NOT save it
if (program.MachineType.MachineTypeId == machineTypeId)
{
update.Program.Add(program);
break; //When debugging it comes here everytime
}
}
//Here it is always: update.Program.Count = 1
database.AddToUpdates(update);
database.SaveChanges();
if (update.MachineType.Count == 0 || update.Program.Count == 0)
MessageBox.Show("This error is nevers shown!");
}
问题是,它总是说它有一个程序,但实际上它没有。我做错了什么,它不会将程序添加到我的更新中?
编辑VARAK:替换代码后它仍然不会保存它,最后也添加了这个
reloadDatabase();
foreach(Update update in database.Updates)
if (update.Program.Count == 0)
MessageBox.Show("This can't be happening");