private void Button_Click(object sender, RoutedEventArgs e) { using (MoviesDBEntities ctx = new MoviesDBEntities()) { try { var movies = (from m in ctx.Movies where m.Title == tbAM.Text select m).First(); Movie = movies; } catch (Exception) { AddMovie window = new AddMovie(); window.ShowDialog(); window.tbTitle.Text = tbAM.Text; window.tbTitle.IsEnabled = false; var movies = (from m in ctx.Movies where m.Title == window.tbTitle.Text select m).First(); Movie = movies; } if ((bool)rbActor.IsChecked) { try { var actor = (from m in ctx.Actors where m.FirstName == tbFN.Text && m.LastName == tbLN.Text select m).First(); actor.Movies.Add(Movie); Movie.Actors.Add(actor); ctx.SaveChanges(); this.Close(); } catch (Exception) { MessageBox.Show("Actor dosen't exist."); this.Close(); } } else if ((bool)rbActress.IsChecked) { try { var actress = (from m in ctx.Actresses where m.FirstName == tbFN.Text && m.LastName == tbLN.Text select m).First(); actress.Movies.Add(Movie); Movie.Actresses.Add(actress); ctx.SaveChanges(); this.Close(); } catch (Exception) { MessageBox.Show("Actress dosen't exist."); this.Close(); } } else if ((bool)rbDirector.IsChecked) { try { var director = (from m in ctx.Directors where m.FirstName == tbFN.Text && m.LastName == tbLN.Text select m).First(); director.Movies.Add(Movie); Movie.Director = director; ctx.SaveChanges(); this.Close(); } catch (Exception) { MessageBox.Show("Director dosen't exist."); this.Close(); } } } }