コード例 #1
0
        public Models.Movie Update(Models.Movie movieModel)
        {
            if (movieModel.Format == null)
            {
                throw new ArgumentNullException("Format", "The Format property must not be null.");
            }
            if (movieModel.Director == null)
            {
                throw new ArgumentNullException("Director", "The Director property must not be null.");
            }

            var existing = db.Movies.SingleOrDefault(x => x.Id == movieModel.Id);

            if (existing == null)
            {
                return(null);
            }

            existing.Cost        = movieModel.Cost;
            existing.Description = movieModel.Description;
            existing.Title       = movieModel.Title;
            existing.ImagePath   = movieModel.ImagePath;

            Data.Director existingDirectors = db.Directors.SingleOrDefault(x => x.Id == movieModel.Director.Id);
            existing.Director = existingDirectors ?? throw new ArgumentException("The associated Director cannot be found.");

            Data.Format existingFormats = db.Formats.SingleOrDefault(x => x.Id == movieModel.Format.Id);
            existing.Format = existingFormats ?? throw new ArgumentException("The associated format cannot be found.");

            Data.Rating existingRatings = db.Ratings.SingleOrDefault(x => x.Id == movieModel.Rating.Id);
            existing.Rating = existingRatings ?? throw new ArgumentException("The associated Rating cannot be found.");

            db.SaveChanges();
            return(movieModel);
        }
コード例 #2
0
        public int Add(Models.Movie movieModel)
        {
            if (movieModel.Format == null)
            {
                throw new ArgumentNullException("Format", "The Format property must not be null.");
            }
            if (movieModel.Director == null)
            {
                throw new ArgumentNullException("Director", "The Director property must not be null.");
            }

            Data.Movie newRow = new Data.Movie();
            newRow.Cost        = movieModel.Cost;
            newRow.Description = movieModel.Description;
            newRow.Title       = movieModel.Title;
            newRow.ImagePath   = movieModel.ImagePath;

            Data.Rating existingRatings = db.Ratings.SingleOrDefault(x => x.Id == movieModel.Rating.Id);
            newRow.Rating = existingRatings ?? throw new ArgumentException("The associated rating cannot be found.");

            Data.Director existingDirectors = db.Directors.SingleOrDefault(x => x.Id == movieModel.Director.Id);
            newRow.Director = existingDirectors ?? throw new ArgumentException("The associated Director cannot be found.");

            Data.Format existingFormats = db.Formats.SingleOrDefault(x => x.Id == movieModel.Format.Id);
            newRow.Format = existingFormats ?? throw new ArgumentException("The associated format cannot be found.");

            db.Movies.Add(newRow);
            db.SaveChanges();
            return(newRow.Id);
        }
コード例 #3
0
        public int Create(Models.Director director)
        {
            Data.Director newDirector = new Data.Director {
                FirstName = director.FirstName, LastName = director.LastName
            };
            db.Directors.Add(newDirector);

            db.SaveChanges();
            return(newDirector.Id);
        }