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);
        }
Esempio n. 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
                        });
                    }
                }
            }
        }
Esempio n. 3
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity <Movie>().HasKey(m => m.Uri);
            modelBuilder.Entity <Planet>().HasKey(m => m.Uri);
            modelBuilder.Entity <Person>().HasKey(m => m.Uri);
            modelBuilder.Entity <Specie>().HasKey(m => m.Uri);
            modelBuilder.Entity <Vehicle>().HasKey(m => m.Uri);
            modelBuilder.Entity <StarShip>().HasKey(m => m.Uri);

            modelBuilder.Entity <Movie>()
            .Ignore(movie => movie.PlanetUris)
            .Ignore(m => m.CharacterUris)
            .Ignore(m => m.StarshipUris)
            .Ignore(m => m.VehicleUris)
            .Ignore(m => m.SpeciesUri);
            modelBuilder.Entity <Planet>()
            .Ignore(planet => planet.MovieUris)
            .Ignore(planet => planet.ResidentsUris);
            modelBuilder.Entity <Person>()
            .Ignore(person => person.MovieUris)
            .Ignore(person => person.VehiclesUris)
            .Ignore(person => person.StarshipUris)
            .Ignore(person => person.SpeciesUris);

            modelBuilder.Entity <Specie>()
            .Ignore(specie => specie.MovieUris)
            .Ignore(specie => specie.HomeworldUri)
            .Ignore(specie => specie.PeopleUris);
            modelBuilder.Entity <Vehicle>()
            .Ignore(vehicle => vehicle.MovieUris)
            .Ignore(vehicle => vehicle.PilotUris);
            modelBuilder.Entity <StarShip>()
            .Ignore(starShip => starShip.MovieUris)
            .Ignore(starShip => starShip.PilotUris);

            modelBuilder.Entity <MoviePlanet>().HasKey(moviePlanet => new { moviePlanet.MovieUri, moviePlanet.PlanetUri });
            modelBuilder.Entity <MovieCharacter>()
            .HasKey(movieCharacter => new { movieCharacter.MovieUri, movieCharacter.PersonUri });
            modelBuilder.Entity <StarshipMovie>()
            .HasKey(starshipMovie => new { starshipMovie.MovieUri, starshipMovie.StarshipUri });
            modelBuilder.Entity <SpecieMovie>()
            .HasKey(specieMovie => new { specieMovie.MovieUri, specieMovie.SpecieUri });
            modelBuilder.Entity <VehicleMovie>()
            .HasKey(vehicleMovie => new { vehicleMovie.MovieUri, vehicleMovie.VehicleUri });

            modelBuilder.Entity <PeopleSpecies>()
            .HasKey(peopleSpecies => new { peopleSpecies.PersonUri, peopleSpecies.SpecieUri });
            modelBuilder.Entity <PeopleStarships>()
            .HasKey(peopleSpecies => new { peopleSpecies.PersonUri, peopleSpecies.StarShipUri });
            modelBuilder.Entity <PeopleVehicles>()
            .HasKey(peopleSpecies => new { peopleSpecies.PersonUri, peopleSpecies.VehicleUri });


            var remoteRepository = new MovieApiRepository();

            IList <Movie> remoteMovies = remoteRepository.GetAllMovies();

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

            IList <Planet> remotePlanets = remoteRepository.GetAllPlanets();

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

            IList <Person> remotePeople = remoteRepository.GetAllPeople();

            modelBuilder.Entity <Person>().HasData(remotePeople.ToArray());

            IList <Specie> remoteSpecies = remoteRepository.GetAllSpecies();

            modelBuilder.Entity <Specie>().HasData(remoteSpecies.ToArray());

            IList <Vehicle> remoteVehicles = remoteRepository.GetAllVehicles();

            modelBuilder.Entity <Vehicle>().HasData(remoteVehicles.ToArray());

            IList <StarShip> remoteStarships = remoteRepository.GetAllStarships();

            modelBuilder.Entity <StarShip>().HasData(remoteStarships.ToArray());

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

                foreach (var person in remotePeople)
                {
                    if (movie.CharacterUris.Contains(person.Uri))
                    {
                        modelBuilder.Entity <MovieCharacter>().HasData(new MovieCharacter()
                        {
                            MovieUri = movie.Uri, PersonUri = person.Uri
                        });
                    }
                }

                foreach (var starShip in remoteStarships)
                {
                    if (movie.StarshipUris.Contains(starShip.Uri))
                    {
                        modelBuilder.Entity <StarshipMovie>().HasData(new StarshipMovie()
                        {
                            MovieUri = movie.Uri, StarshipUri = starShip.Uri
                        });
                    }
                }

                foreach (var vehicle in remoteVehicles)
                {
                    if (movie.VehicleUris.Contains(vehicle.Uri))
                    {
                        modelBuilder.Entity <VehicleMovie>().HasData(new VehicleMovie()
                        {
                            MovieUri = movie.Uri, VehicleUri = vehicle.Uri
                        });
                    }
                }

                foreach (var specie in remoteSpecies)
                {
                    if (movie.SpeciesUri.Contains(specie.Uri))
                    {
                        modelBuilder.Entity <SpecieMovie>().HasData(new SpecieMovie()
                        {
                            MovieUri = movie.Uri, SpecieUri = specie.Uri
                        });
                    }
                }
            }

            foreach (var person in remotePeople)
            {
                foreach (var specie in remoteSpecies)
                {
                    if (specie.PeopleUris.Contains(person.Uri))
                    {
                        modelBuilder.Entity <PeopleSpecies>().HasData(new PeopleSpecies()
                        {
                            PersonUri = person.Uri, SpecieUri = specie.Uri
                        });
                    }
                }

                foreach (var vehicle in remoteVehicles)
                {
                    if (vehicle.PilotUris.Contains(person.Uri))
                    {
                        modelBuilder.Entity <PeopleVehicles>().HasData(new PeopleVehicles()
                        {
                            PersonUri = person.Uri, VehicleUri = vehicle.Uri
                        });
                    }
                }

                foreach (var starShip in remoteStarships)
                {
                    if (starShip.PilotUris.Contains(person.Uri))
                    {
                        modelBuilder.Entity <PeopleStarships>().HasData(new PeopleStarships()
                        {
                            PersonUri = person.Uri, StarShipUri = starShip.Uri
                        });
                    }
                }
            }
        }