private void SaveToDatabase() { MusicAlbumsGuideDB context = new MusicAlbumsGuideDB(); Album newAlbum = new Album(); newAlbum.Name = NameTextBox.Text; Genre currentGenre = context.Genres.Where(p => p.Name == GenreComboBox.Text).Single(); newAlbum.GenreId = currentGenre.GenreId; newAlbum.ReleaseYear = Convert.ToInt32(ReleaseYearTextBox.Text); Carrier currentCarrier = context.Carriers.Where(p => p.Name == CarrierComboBox.Text).Single(); newAlbum.CarrierId = currentCarrier.CarrierId; // Database.Models.Type currentType = context.Types.Where(p => p.Name == TypeComboBox.Text).Single(); newAlbum.TypeId = currentType.TypeId; Author author; try { author = context.Authors.Where(p => p.Name == AuthorTextBox.Text).Single(); } catch (Exception) { context.Authors.Add(new Author() { Name = AuthorTextBox.Text }); context.SaveChanges(); author = context.Authors.Where(p => p.Name == AuthorTextBox.Text).Single(); } newAlbum.AuthorId = author.AuthorId; newAlbum.TrackCount = Convert.ToInt32(TrackCountTextBox.Text); // ReleasingLabel releasingLabel; try { releasingLabel = context.ReleasingLabels.Where(p => p.Name == ReleaseLabelTextBox.Text).Single(); } catch (Exception) { context.ReleasingLabels.Add(new ReleasingLabel() { Name = ReleaseLabelTextBox.Text }); context.SaveChanges(); releasingLabel = context.ReleasingLabels.Where(p => p.Name == ReleaseLabelTextBox.Text).Single(); } newAlbum.ReleasingLabelId = releasingLabel.ReleasingLabelId; newAlbum.CoverPhoto = ImageMethods.ConvertImageToByteArray(CoverAlbum.Image); context.Albums.Add(newAlbum); context.SaveChanges(); }