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(); }
private void Delete_Click(object sender, EventArgs e) { MusicAlbumsGuideDB context = new MusicAlbumsGuideDB(); Album albumToDelete = context.Albums.Include(p => p.ReleasingLabel).Include(p => p.Author).Include(p => p.Carrier).Include(p => p.Genre).Include(p => p.Type).Where(p => p.AlbumId == currentAlbum.AlbumId).Single(); ReleasingLabel currentReleasingLabel = albumToDelete.ReleasingLabel; string labelName = ""; string authName = ""; if (currentReleasingLabel != null) { if (context.Albums.Where(p => p.ReleasingLabel.ReleasingLabelId == currentReleasingLabel.ReleasingLabelId).ToList().Count == 1) { labelName = albumToDelete.ReleasingLabel.Name; } } // Author currentAuth = albumToDelete.Author; if (currentAuth != null) { if (context.Albums.Where(p => p.Author.AuthorId == currentAuth.AuthorId).ToList().Count == 1) { authName = currentAuth.Name; } } // context.Albums.Remove(albumToDelete); context.SaveChanges(); if (labelName != "") { context.ReleasingLabels.Remove(context.ReleasingLabels.Where(p => p.Name == labelName).Single()); } context.SaveChanges(); if (authName != "") { context.Authors.Remove(context.Authors.Where(p => p.Name == authName).Single()); } context.SaveChanges(); parentForm.HomeButton.PerformClick(); }
protected override void Seed(MusicAlbumsGuideDB context) { //List<Lot> lots = new List<Lot>() { new Lot { Name = "Мультиварка Bosch", State = "б/у", StartDate = DateTime.Now, FinishDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 2) } }; //for (int i = 0; i < lots.Count; i++) //{ // context.Lots.Add(lots[i]); //} //context.SaveChanges(); List <Carrier> carriers = new List <Carrier>() { new Carrier() { Name = "Винил" }, new Carrier() { Name = "Аудиокассеты" }, new Carrier() { Name = "Цифровой носитель" }, new Carrier() { Name = "CD диск" } }; for (int i = 0; i < carriers.Count; i++) { context.Carriers.Add(carriers[i]); } context.SaveChanges(); List <Genre> genres = new List <Genre>() { new Genre() { Name = "oldschool rap" }, new Genre() { Name = "newschool rap" }, new Genre() { Name = "gangsta rap" }, new Genre() { Name = "horrorcore" }, new Genre() { Name = "south hip-hop" }, new Genre() { Name = "rock" }, new Genre() { Name = "punk-rock" }, new Genre() { Name = "hardcore" }, new Genre() { Name = "post-hardcore" }, new Genre() { Name = "metalcore" }, new Genre() { Name = "emo" }, new Genre() { Name = "screamo" }, new Genre() { Name = "punk" }, new Genre() { Name = "post-punk" }, new Genre() { Name = "metal" }, new Genre() { Name = "nu-metal" }, new Genre() { Name = "heavy metal" }, new Genre() { Name = "trash metal" }, new Genre() { Name = "black metal" }, new Genre() { Name = "death metal" }, new Genre() { Name = "slamming brutal death" }, new Genre() { Name = "funeral doom" }, new Genre() { Name = "doom metal" }, new Genre() { Name = "EDM" }, new Genre() { Name = "techno" }, new Genre() { Name = "house" }, new Genre() { Name = "tranсe" }, new Genre() { Name = "synthpop" }, new Genre() { Name = "ambient" }, new Genre() { Name = "noise" }, new Genre() { Name = "noise-rock" }, new Genre() { Name = "powernoise" }, new Genre() { Name = "harsh noise" }, new Genre() { Name = "classic" }, new Genre() { Name = "jazz" }, new Genre() { Name = "blues" }, }; for (int i = 0; i < genres.Count; i++) { context.Genres.Add(genres[i]); } context.SaveChanges(); List <Models.Type> types = new List <Models.Type>() { new Models.Type() { Name = "Полноформатный" }, new Models.Type() { Name = "EP" }, new Models.Type() { Name = "Сингл" }, new Models.Type() { Name = "Демо" }, new Models.Type() { Name = "Сплит" } }; for (int i = 0; i < types.Count; i++) { context.Types.Add(types[i]); } context.SaveChanges(); context.SaveChanges(); }