示例#1
0
        /// <summary>
        /// Inserts new rating and calculates rating for the movie. There is no changing of this data, therefore only insert exists.
        /// </summary>
        /// <param name="rating"></param>
        /// <returns></returns>
        public decimal InsertMovieRating(Rating rating)
        {
            rating.Movie = null;
            movieDB.Rating.Add(rating);
            movieDB.SaveChanges();
            Dictionary <int, int> ratings = movieDB.SqlQuery <int, int>(System.Data.CommandType.Text, "SELECT SUM(Rating) as Rating,COUNT(MovieID) as Movies FROM Rating WHERE MovieID = " + rating.MovieId, "Rating", "Movies") as Dictionary <int, int>;
            Movie movie = movieDB.Movie.Find(rating.MovieId);

            movie.Rating = (decimal)ratings.FirstOrDefault().Key / (decimal)ratings.FirstOrDefault().Value;
            movieDB.Entry(movie).State = EntityState.Modified;
            movieDB.SaveChanges();
            return(movie.Rating);
        }