private void button3_Click(object sender, EventArgs e) { using (var db = new Model2()) { // creates/adds video and playlist items in db with many to many association. var newVideo = new Video(); var newPlaylist = new playlist(); newVideo.Name = "New Video"; newPlaylist.Name = "New Playlist"; //uses many to many relationships for both video and playlist newVideo.Playlists = new List <playlist>() { newPlaylist }; newPlaylist.Videos = new List <Video>() { newVideo }; //updates log addLineToLog("New Video: " + newVideo.Name); addLineToLog("New Playlist: " + newPlaylist.Name); addLineToLog("Many2Many association created for " + newVideo.Name + " and " + newPlaylist.Name); //adds both the video and playlist to the db as well as their many to many association (table: videoplaylists) db.Videos.Add(newVideo); db.playlists.Add(newPlaylist); db.SaveChanges(); } }
private void button4_Click(object sender, EventArgs e) { // many to many relationship. many different videos can be on many different playlists int tempID; //grabs the user input for the Video's ID to use in this example. *caution* currently Does not check for bad input tempID = Convert.ToInt32(textBox2.Text); //uses the model2 db (manytomany.db) using (var db = new Model2()) { //finds the video the ID of the users input tempID var tempVideo = db.Videos.Where(v => v.ID == tempID).Include(v => v.Playlists).FirstOrDefault(); //lists all of the names of returned video's playlists addLineToLog("Video: " + tempVideo.Name + " can be found on the following playlists:"); foreach (var playlist in tempVideo.Playlists) { //MessageBox.Show(playlist.Name); addLineToLog(playlist.Name); } //removes any playlist's association with the returned video (table: videoplaylists) tempVideo.Playlists.Clear(); db.SaveChanges(); } }