//подгружаем предыдущие данные для редактирования private void LoadPrevData() { using (RconfigContext context = new RconfigContext()) { var queryPrevFavorite = (from c in context.Favorites where c.Hostname == prevFavName select c).FirstOrDefault(); //если избранное было найдено подгружаем данные if (queryPrevFavorite != null) { //сохраняем наше избранное currentFavorite = queryPrevFavorite; //редактируем данные формы textBoxHostname.Text = prevFavName; textBoxAddress.Text = queryPrevFavorite.Address; numericUpDownPort.Value = queryPrevFavorite.Port; numericUpDownTimeOut.Value = queryPrevFavorite.TimeOut; //проверить var credential = queryPrevFavorite.Credential.CredentialName; var location = queryPrevFavorite.Location.LocationName; var protocol = queryPrevFavorite.Protocol.Name; var category = queryPrevFavorite.Category.CategoryName; comboBoxCredential.SelectedItem = credential; comboBoxLocation.SelectedItem = location; comboBoxProtocol.SelectedItem = protocol; comboBoxCategory.SelectedItem = category; } else { //если в базе данных не удалось обнаружить избранное с указанным именем, то оставляем форму без изменений mode = WindowsMode.ADD; } } }
//импорт в базу данных private void DataImport() { using (RconfigContext context = new RconfigContext()) { bool attributeLoadSuccess = true; //данные по местоположению string location = comboBoxLocations.SelectedValue.ToString(); var queryLocation = (from c in context.Locations where c.LocationName == location select c).FirstOrDefault(); if (queryLocation == null) attributeLoadSuccess = false; //данные безопасности string credential = comboBoxCredentials.SelectedValue.ToString(); var queryCredential = (from c in context.Credentials where c.CredentialName == credential select c).FirstOrDefault(); if (queryCredential == null) attributeLoadSuccess = false; //данные протокола string protocol = comboBoxProtocols.SelectedValue.ToString(); var queryProtocol = (from c in context.Protocols where c.Name == protocol select c).FirstOrDefault(); if (queryProtocol == null) attributeLoadSuccess = false; //данные категории string category = comboBoxCategories.SelectedValue.ToString(); var queryCategory = (from c in context.Categories where c.CategoryName == category select c).FirstOrDefault(); if (queryCategory == null) attributeLoadSuccess = false; int port = (int)numericUpDownPort.Value; //если успешно импортировать устройства if (attributeLoadSuccess) { foreach (string address in network.Keys) { string hostname = network[address].ToString(); Favorite fav = new Favorite(); fav.Hostname = hostname; fav.Address = address; fav.Port = port; fav.Protocol = queryProtocol; fav.Location = queryLocation; fav.Credential = queryCredential; fav.Category = queryCategory; fav.Date = DateTime.Now; context.Favorites.Add(fav); } context.SaveChanges(); } else { MessageBox.Show("Attributes load failed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
//добавляем избранное в базу private void FavoriteAdd() { using (RconfigContext context = new RconfigContext()) { currentFavorite = new Favorite(); currentFavorite.Hostname = textBoxHostname.Text.Trim(); currentFavorite.Address = textBoxAddress.Text.Trim(); currentFavorite.Port = (int)numericUpDownPort.Value; currentFavorite.TimeOut=(int)numericUpDownTimeOut.Value; currentFavorite.Date = DateTime.UtcNow; //данные по местоположению string location = comboBoxLocation.SelectedValue.ToString(); var queryLocation = (from c in context.Locations where c.LocationName == location select c).FirstOrDefault(); if (queryLocation != null) currentFavorite.Location = queryLocation; //данные безопасности string credential = comboBoxCredential.SelectedValue.ToString(); var queryCredential = (from c in context.Credentials where c.CredentialName == credential select c).FirstOrDefault(); if (queryCredential != null) currentFavorite.Credential = queryCredential; //данные протокола string protocol = comboBoxProtocol.SelectedValue.ToString(); var queryProtocol = (from c in context.Protocols where c.Name == protocol select c).FirstOrDefault(); currentFavorite.Protocol = queryProtocol; //данные категории string category = comboBoxCategory.SelectedValue.ToString(); var queryCategory = (from c in context.Categories where c.CategoryName == category select c).FirstOrDefault(); currentFavorite.Category = queryCategory; //добавляем избранное в базу данных context.Favorites.Add(currentFavorite); context.SaveChanges();//??? } }