0

所以我有一个这个模型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");
4

2 回答 2

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));

        var programVal = row.Cells[5].Value.ToString() + "v";

        var program = database.Programs.FirstOrDefault(prog => prog.ProgramVersion.StartsWith(programVal));

        if (program == null)
            program = database.Programs.First(prog => prog.MachineType.MachineTypeId == machineTypeId);

        update.Program.Add(program);            

        database.AddToUpdates(update);
        database.SaveChanges();

        //This will never show an error as the update that you are accessing here is the one you created in memory (Not the one in the DB)
        if (update.MachineType.Count == 0 || update.Program.Count == 0)
            MessageBox.Show("This will never show!");

        //To get the error, pull the update from the database again
        var updateNew = database.Updates.First(upd => upd.UpdateId == update.UpdateId);
        if (updateNew.MachineType.Count == 0 || updateNew.Program.Count == 0)
            MessageBox.Show("This will show if prog was not saved");

    }

这段代码应该更有效,如果它不起作用,可能会更清楚一点。

于 2013-03-11T10:10:41.733 回答
0

终于找到了解决方案,正如您在我的模型中看到的那样,程序可以有 1 个更新,因此当使用与另一个更新相同的程序创建新更新时,其他更新将失去连接的程序。

于 2013-03-27T14:50:57.900 回答