public Film ToModel(star_wars_apiContext context)
        {
            Film film = context.Film.Find(this.id);

            bool newObject = false;

            if (film == null)
            {
                newObject         = true;
                film              = new Film();
                film.characterIds = new List <FilmCharacter>();
                film.planetIds    = new List <FilmPlanet>();
                film.speciesIds   = new List <FilmSpecies>();
                film.starshipIds  = new List <FilmStarship>();
                film.vehicleIds   = new List <FilmVehicle>();
            }

            film.id           = this.id;
            film.created      = this.created;
            film.edited       = this.edited;
            film.director     = this.director;
            film.episodeId    = this.episodeId;
            film.openingCrawl = this.openingCrawl;
            film.producer     = this.producer;
            film.releaseDate  = this.releaseDate;
            film.title        = this.title;

            if (newObject == false)
            {
                // many-many mapping classes have the IDs as foreign keys - if the object does not yet exist it cannot be linked to other objects.
                foreach (int characterId in this.characterIds)
                {
                    if (context.Character.Find(characterId) != null)
                    {
                        FilmCharacter filmCharacter = context.FilmCharacter.Find(this.id, characterId);
                        if (filmCharacter == null)
                        {
                            filmCharacter = new FilmCharacter(this.id, characterId);
                        }
                        if (film.characterIds.Contains(filmCharacter) == false)
                        {
                            film.characterIds.Add(filmCharacter);
                        }
                    }
                }

                foreach (int planetId in this.planetIds)
                {
                    if (context.Planet.Find(planetId) != null)
                    {
                        FilmPlanet filmPlanet = context.FilmPlanet.Find(this.id, planetId);
                        if (filmPlanet == null)
                        {
                            filmPlanet = new FilmPlanet(this.id, planetId);
                        }
                        if (film.planetIds.Contains(filmPlanet) == false)
                        {
                            film.planetIds.Add(filmPlanet);
                        }
                    }
                }

                foreach (int speciesId in this.speciesIds)
                {
                    if (context.Species.Find(speciesId) != null)
                    {
                        FilmSpecies filmSpecies = context.FilmSpecies.Find(this.id, speciesId);
                        if (filmSpecies == null)
                        {
                            filmSpecies = new FilmSpecies(this.id, speciesId);
                        }
                        if (film.speciesIds.Contains(filmSpecies) == false)
                        {
                            film.speciesIds.Add(filmSpecies);
                        }
                    }
                }

                foreach (int starshipId in this.starshipIds)
                {
                    if (context.Starship.Find(starshipId) != null)
                    {
                        FilmStarship filmStarship = context.FilmStarship.Find(this.id, starshipId);
                        if (filmStarship == null)
                        {
                            filmStarship = new FilmStarship(this.id, starshipId);
                        }
                        if (film.starshipIds.Contains(filmStarship) == false)
                        {
                            film.starshipIds.Add(filmStarship);
                        }
                    }
                }

                foreach (int vehicleId in this.vehicleIds)
                {
                    if (context.Vehicle.Find(vehicleId) != null)
                    {
                        FilmVehicle filmVehicle = context.FilmVehicle.Find(this.id, vehicleId);
                        if (filmVehicle == null)
                        {
                            filmVehicle = new FilmVehicle(this.id, vehicleId);
                        }
                        if (film.vehicleIds.Contains(filmVehicle) == false)
                        {
                            film.vehicleIds.Add(filmVehicle);
                        }
                    }
                }
            }
            return(film);
        }
        public Vehicle ToModel(star_wars_apiContext context)
        {
            Vehicle Vehicle = context.Vehicle.Find(this.id);

            bool newObject = false;

            if (Vehicle == null)
            {
                newObject = true;
                Vehicle   = new Vehicle();
            }

            Vehicle.id            = this.id;
            Vehicle.created       = this.created;
            Vehicle.edited        = this.edited;
            Vehicle.vehicleClass  = this.vehicleClass;
            Vehicle.cargoCapacity = this.cargoCapacity;
            Vehicle.consumables   = this.consumables;
            Vehicle.costInCredits = this.costInCredits;
            Vehicle.crew          = this.crew;
            Vehicle.length        = this.length;
            Vehicle.manufacturer  = this.manufacturer;
            Vehicle.model         = this.model;
            Vehicle.name          = this.name;
            Vehicle.passengers    = this.passengers;
            Vehicle.filmIds       = new List <FilmVehicle>();
            Vehicle.pilotIds      = new List <VehicleCharacter>();

            if (newObject == false)
            {
                // many-many mapping classes have the IDs as foreign keys - if the object does not yet exist it cannot be linked to other objects.
                foreach (int filmId in this.filmIds)
                {
                    if (context.Film.Find(filmId) != null)
                    {
                        FilmVehicle filmVehicle = context.FilmVehicle.Find(filmId, this.id);
                        if (filmVehicle == null)
                        {
                            Vehicle.filmIds.Add(new FilmVehicle(filmId, this.id));
                        }
                        else
                        {
                            Vehicle.filmIds.Add(filmVehicle);
                        }
                    }
                }

                foreach (int characterId in this.pilotIds)
                {
                    if (context.Character.Find(characterId) != null)
                    {
                        VehicleCharacter vehicleCharacter = context.VehicleCharacter.Find(this.id, characterId);
                        if (vehicleCharacter == null)
                        {
                            Vehicle.pilotIds.Add(new VehicleCharacter(this.id, characterId));
                        }
                        else
                        {
                            Vehicle.pilotIds.Add(vehicleCharacter);
                        }
                    }
                }
            }
            return(Vehicle);
        }