Ejemplo n.º 1
0
        public static void SeedMovies(IMovieRepository movierepo, IMovieGenreRepository moviegenrerepo)
        {
            using (FileStream fs = File.Open("DataInit\\movies.csv", FileMode.Open, FileAccess.Read, FileShare.None))
            {
                var movie_parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(fs);
                movie_parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                movie_parser.SetDelimiters(new string[] { "," });

                //movierepo.Clear();
                //movierepo.SetIdentityInsert(true);

                bool              first       = true;
                List <Movie>      movies      = new List <Movie>();
                List <MovieGenre> movieGenres = new List <MovieGenre>();
                while (!movie_parser.EndOfData)
                {
                    //0: index, 1: movieId, 2: title, 3: overview, 4: poster_path, 5: video_key, ** genresId
                    string[] row = movie_parser.ReadFields();
                    if (first)
                    {
                        first = false;
                        continue;
                    }

                    var movie = new Movie
                    {
                        MovieId         = Int32.Parse(row[1]),
                        Title           = row[2],
                        SortDescription = row[3],
                        LongDescription = row[3],
                        PosterPath      = Path.Combine("\\posters", row[4].Substring(1)), // ignore 1st char '/'
                        YoutubeId       = row[5]
                    };

                    string[] gerne_ids = row[6].Split(',');
                    for (int i = 0; i < gerne_ids.Length; i++)
                    {
                        movieGenres.Add(new MovieGenre
                        {
                            GenreId = Int32.Parse(gerne_ids[i]),
                            MovieId = movie.MovieId
                        });
                    }


                    movies.Add(movie);
                }
                movierepo.InsertBulk(movies);
                moviegenrerepo.InsertBulk(movieGenres);
                //movierepo.SetIdentityInsert(false);
            }
        }