private void bAdd_Click(object sender, EventArgs e) { if (tBName.Text != "" && nUDLength.Value > 0) { MusicDataEF.Songs song = new MusicDataEF.Songs(); song.S_name = tBName.Text; song.S_length = (int)nUDLength.Value; song.S_albId = (int)cBAlbum.SelectedValue; ctx.Songs.Add(song); ctx.SaveChanges(); foreach (var ganre in lBGanres.SelectedItems) { MusicDataEF.SongGanre con = new MusicDataEF.SongGanre(); con.SG_ganreId = ((MusicDataEF.Ganres)ganre).G_id; con.SG_songId = song.S_id; ctx.SongGanre.Add(con); ctx.SaveChanges(); } Close(); } else { MessageBox.Show("Wrong information"); } }
private void bAdd_Click(object sender, EventArgs e) { var unsaved = ctx.ChangeTracker.Entries <MusicDataEF.SongGanre>().Where(a => a.State != EntityState.Unchanged).ToList(); if (unsaved.Count != 0) { DialogResult result = MessageBox.Show("The data has been not saved yet, do you want to save?", "Warning", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { ctx.SaveChanges(); } else { return; } } int g_id = (int)cBGanres.SelectedValue; int s_id = song.S_id; var exist = (from c in ctx.SongGanre where (g_id == c.SG_ganreId) && (s_id == c.SG_songId) select c).Any(); if (exist) { MessageBox.Show("This song has already had this ganre"); } else { MusicDataEF.SongGanre sg = new MusicDataEF.SongGanre(); sg.SG_songId = s_id; sg.SG_ganreId = g_id; ctx.SongGanre.Add(sg); } }