public FilmLocations addLocationToFilm(string filmName, string locationName, string xCoord, string yCoord) { FilmLocations filmToModify = findFilmLocationByName(filmName); List<Location> locsInFilm = null; if (filmToModify != null) { // Create a location with dependent Point object. This will auto-persist the dependents Location location = new Location(locationName, new Point(xCoord, yCoord)); // If there is no list, create one if (filmToModify.locations != null) locsInFilm = filmToModify.locations.ToList(); else locsInFilm = new List<Location>(); // Add the new location to the existing list.. locsInFilm.Add(location); // ... and save it back into the object filmToModify.locations = locsInFilm; } // EF will only save the main object (and hence the dependents) if it is marked as modified db.Entry(filmToModify).State = EntityState.Modified; db.SaveChanges(); // Check it comes back (legacy) FilmLocations x = findFilmLocationByName(filmName); return filmToModify; }
public Location addLocation(string locationName, string xCoord, string yCoord) { PointDAL pointDAL = new PointDAL(); Point point = pointDAL.addPoint(xCoord, yCoord); Location location = new Location(locationName, point); db.locations.Add(location); return location; }