private void bAdd_Click(object sender, EventArgs e) { if (tBName.Text != "") { MusicDataEF.MusicPlayers mp = new MusicDataEF.MusicPlayers(); mp.MP_name = tBName.Text; ctx.MusicPlayers.Add(mp); ctx.SaveChanges(); foreach (var art in lBArtist.SelectedItems) { MusicDataEF.ArtistPlayer con = new MusicDataEF.ArtistPlayer(); //DataRowView drv = (DataRowView)lBArtist.Items[(int)id]; //artists.Add((int)drv["A_id"]); //MessageBox.Show(id.ToString() + " " + drv["A_id"].ToString()); con.AP_artId = ((MusicDataEF.Artists)art).A_id; con.Ap_playerId = mp.MP_id; ctx.ArtistPlayer.Add(con); ctx.SaveChanges(); } Close(); } else { MessageBox.Show("Wrong information"); } }
private void bAdd_Click(object sender, EventArgs e) { var unsaved = ctx.ChangeTracker.Entries <MusicDataEF.ArtistPlayer>().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 a_id = (int)cBArtist.SelectedValue; int mp_id = mp.MP_id; var exist = (from c in ctx.ArtistPlayer where (a_id == c.AP_artId) && (mp_id == c.Ap_playerId) select c).Any(); if (exist) { MessageBox.Show("This player has already presented this artist"); } else { MusicDataEF.ArtistPlayer ap = new MusicDataEF.ArtistPlayer(); ap.AP_artId = a_id; ap.Ap_playerId = mp_id; ctx.ArtistPlayer.Add(ap); } }