protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity <Movie>().HasKey(m => m.URI);

            IList <Movie> remoteMovies = new MovieApiRepository().GetAllMovies();

            modelBuilder.Entity <Movie>().HasData(remoteMovies.ToArray());
            base.OnModelCreating(modelBuilder);
        }
Example #2
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Define Primary Keys
            modelBuilder.Entity <Movie>().HasKey(movie => movie.Uri);
            modelBuilder.Entity <Planet>().HasKey(planet => planet.Uri);

            // Don't map lists of Uri, this is modeled in MoviePlanet entity
            modelBuilder.Entity <Movie>().Ignore(movie => movie.PlanetUris);
            modelBuilder.Entity <Planet>().Ignore(planet => planet.MovieUris);

            // Configure many to many
            modelBuilder.Entity <MoviePlanet>().HasKey(moviePlanet => new
            {
                moviePlanet.MovieUri, moviePlanet.PlanetUri
            });

            // Seed Data
            IList <Movie> remoteMovies = new MovieApiRepository().GetAllMovies();

            modelBuilder.Entity <Movie>().HasData(remoteMovies.ToArray());

            IList <Planet> remotePlanets = new PlanetApiRepository().GetAllPlanets();

            modelBuilder.Entity <Planet>().HasData(remotePlanets.ToArray());

            foreach (var movie in remoteMovies)
            {
                foreach (var planet in remotePlanets)
                {
                    // planet appears in movie
                    if (movie.PlanetUris.Contains(planet.Uri))
                    {
                        modelBuilder.Entity <MoviePlanet>().HasData(new MoviePlanet()
                        {
                            MovieUri  = movie.Uri,
                            PlanetUri = planet.Uri
                        });
                    }
                }
            }
        }