示例#1
0
 private void Update(int movieId)
 {
     try
     {
         var image = new ImageByteConverter();
         using (var unitOfWork = new UnitOfWork(new MovieBookingContext()))
         {
             var movie = unitOfWork.Movies.Get(movieId);
             movie.Title                = txtTitle.Text;
             movie.Poster               = image.ImageToByte(picPoster.Image);
             movie.ReleaseDate          = dtpReleaseDate.Value;
             movie.Description          = txtDescription.Text;
             movie.Genre                = (Genre)Enum.Parse(typeof(Genre), cboGenre.SelectedItem.ToString());
             movie.ImdbRating           = (float)Convert.ToDecimal(txtIMDBRating.Text);
             movie.RottenTomatoesRating = (float)Convert.ToDecimal(txtRottenTomatoes.Text);
             movie.PgRating             = txtPgRating.Text;
             movie.Trailer              = txtTrailerLink.Text;
             movie.Director             = txtDirector.Text;
             movie.Casts                = txtCast.Text;
             unitOfWork.Complete();
             MessageBox.Show(@"Update Successfull");
         }
     }
     catch (ValidationException ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
示例#2
0
        private void Save()
        {
            using (var unitOfWork = new UnitOfWork(new MovieBookingContext()))
            {
                try
                {
                    var image = new ImageByteConverter();
                    var movie = new Movie
                    {
                        Title                = txtTitle.Text,
                        Poster               = image.ImageToByte(picPoster.Image),
                        ReleaseDate          = dtpReleaseDate.Value,
                        Description          = txtDescription.Text,
                        Genre                = (Genre)Enum.Parse(typeof(Genre), cboGenre.SelectedItem.ToString()),
                        ImdbRating           = (float)Convert.ToDecimal(txtIMDBRating.Text),
                        RottenTomatoesRating = (float)Convert.ToDecimal(txtRottenTomatoes.Text),
                        PgRating             = txtPgRating.Text,
                        Trailer              = txtTrailerLink.Text,
                        Director             = txtDirector.Text,
                        Casts                = txtCast.Text
                    };

                    unitOfWork.Movies.Add(movie);
                    unitOfWork.Complete();
                    txtMovieId.Text = movie.Id.ToString();
                    MessageBox.Show(@"Save Successfull");
                }

                catch (DbEntityValidationException ex)
                {
                    var errorMessages = ex.EntityValidationErrors
                                        .SelectMany(x => x.ValidationErrors)
                                        .Select(x => x.ErrorMessage);

                    // Join the list to a single string.
                    var fullErrorMessage = string.Join("; ", errorMessages);

                    // Combine the original exception message with the new one.
                    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                    // Throw a new DbEntityValidationException with the improved exception message.
                    throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }