private void button2_Click(object sender, EventArgs e)
        {
            if (selected_version_id != -1)
            {
                using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString()))
                {
                    using (var transaction = db.Database.BeginTransaction())
                    {
                        try
                        {
                            db.Network_Connections.RemoveRange(db.Network_Connections.Where(c => c.C_Version == selected_version_id));
                            db.Network_Hardware.RemoveRange(db.Network_Hardware.Where(c => c.C_Version == selected_version_id));
                            db.Network_Settings.RemoveRange(db.Network_Settings.Where(c => c.C_Version == selected_version_id));
                            db.Network_Ports.RemoveRange(db.Network_Ports.Where(c => c.C_Version == selected_version_id));
                            db.Network_Versions.RemoveRange(db.Network_Versions.Where(c => c.ID == selected_version_id));

                            db.SaveChanges();
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Ошибка cохранения версии : " + ex.ToString());
                            transaction.Rollback();
                            return;
                        }
                    }
                }
                workspace.NewScheme();
                GetVersions();
            }
        }
Exemple #2
0
        public void SaveToDB()
        {
            using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString()))
            {
                Network_Versions new_version = new Network_Versions();
                new_version.C_Date           = DateTime.Now.ToString("d/M/yyyy");
                new_version.C_Version_Number = version + 1;
                new_version.ID = version_id + 1;

                List <Network_Hardware>    db_devices     = new List <Network_Hardware>();
                List <Network_Settings>    db_settings    = new List <Network_Settings>();
                List <Network_Ports>       db_ports       = new List <Network_Ports>();
                List <Network_Connections> db_connections = new List <Network_Connections>();

                int connection_id = 0;
                foreach (NetDevice device in devices)
                {
                    Network_Hardware db_device = new Network_Hardware();
                    db_device.ID            = starting_id + device.id_real;
                    db_device.C_Interface_X = device.sprite.Location.X;
                    db_device.C_Interface_Y = device.sprite.Location.Y;
                    db_device.C_Name        = device.name;
                    db_device.C_Type        = device.type;
                    db_device.C_Status      = false;
                    db_device.C_Version     = new_version.ID;

                    db_devices.Add(db_device);

                    foreach (NetPort port in device.ports)
                    {
                        Network_Settings db_setting = new Network_Settings();
                        db_setting.ID                = starting_id + port.settings.id_real;
                        db_setting.C_IP              = port.settings.IP;
                        db_setting.C_Subnet_Mask     = port.settings.subnet_mask;
                        db_setting.C_Default_Gateway = port.settings.default_gateway;
                        db_setting.C_Version         = new_version.ID;

                        db_settings.Add(db_setting);

                        Network_Ports db_port = new Network_Ports();
                        db_port.ID            = starting_id + port.id_real;
                        db_port.C_Status      = port.enabled;
                        db_port.C_Version     = new_version.ID;
                        db_port.C_Hardware_ID = db_device.ID;
                        db_port.C_Settings_ID = db_setting.ID;

                        db_ports.Add(db_port);

                        if (port.connected_port != null)
                        {
                            Network_Connections db_connection = new Network_Connections();
                            db_connection.ID             = starting_id + connection_id;
                            db_connection.C_Version      = new_version.ID;
                            db_connection.C_From_Port_ID = starting_id + port.id_real;
                            db_connection.C_To_Port_ID   = starting_id + port.connected_port.id_real;
                            db_connections.Add(db_connection);

                            connection_id++;
                        }
                    }
                }

                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Network_Versions.Add(new_version);

                        foreach (Network_Settings db_setting in db_settings)
                        {
                            db.Network_Settings.Add(db_setting);
                        }
                        foreach (Network_Hardware db_device in db_devices)
                        {
                            db.Network_Hardware.Add(db_device);
                        }
                        foreach (Network_Ports db_port in db_ports)
                        {
                            db.Network_Ports.Add(db_port);
                        }
                        foreach (Network_Connections db_connection in db_connections)
                        {
                            db.Network_Connections.Add(db_connection);
                        }

                        db.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Ошибка cохранения версии : " + ex.ToString());
                        transaction.Rollback();
                    }
                }
            }
            MessageBox.Show("Успешно сохранена версия : " + (version + 1));
            GetActualVersion();
        }