public ActorCreationData UpdateSec(ActorCreationData actorCreationData) { using (var entityContext = new MovieDbContext()) { // peki existing null ise? var existingActor = (from x in entityContext.ActorSet where x.Id == actorCreationData.Id select x).FirstOrDefault(); SimpleMapper.PropertyMap(actorCreationData, existingActor); var currentMovieActorMappings = entityContext.MovieActorMappingSet.Where(t => t.ActorId == actorCreationData.Id).ToList(); if (actorCreationData.MovieIds == null) { actorCreationData.MovieIds = new int[] {} } ; if (currentMovieActorMappings.Count > actorCreationData.MovieIds.Count()) { for (int i = 0; i < actorCreationData.MovieIds.Count(); i++) { currentMovieActorMappings[i].MovieId = actorCreationData.MovieIds[i]; } //2 for (int i = actorCreationData.MovieIds.Count(); i < currentMovieActorMappings.Count; i++) { entityContext.MovieActorMappingSet.Remove(currentMovieActorMappings[i]); } entityContext.SaveChanges(); } else { int index = 0; foreach (var currentGenreMap in currentMovieActorMappings) { currentGenreMap.MovieId = actorCreationData.MovieIds[index]; index++; } if (actorCreationData.MovieIds.Count() != currentMovieActorMappings.Count) { for (int i = index; i < actorCreationData.MovieIds.Count(); i++) { var newMovieActorMapping = new MovieActorMapping() { ActorId = actorCreationData.Id, MovieId = actorCreationData.MovieIds[i] }; entityContext.MovieActorMappingSet.Add(newMovieActorMapping); } } entityContext.SaveChanges(); } } return(null); }
public MovieData UpdateSimple(MovieData movieData) { using (var entityContext = new MovieDbContext()) { var entity = entityContext.MovieSet.Find(movieData.Id); entity.Name = movieData.Name; entityContext.SaveChanges(); return(movieData); } }
public MovieCreationData Add(MovieCreationData movieCreationData) { using (var entityContext = new MovieDbContext()) { var movie = new Movie(); SimpleMapper.PropertyMap(movieCreationData, movie); movie.CreatedAt = DateTime.Now; entityContext.MovieSet.Add(movie); entityContext.SaveChanges(); if (movieCreationData.GenreIds != null) { foreach (var genreId in movieCreationData.GenreIds) { entityContext.MovieGenreMappingSet.Add(new MovieGenreMapping() { GenreId = genreId, MovieId = movie.Id }); } } if (movieCreationData.SubGenreIds != null) { foreach (var subGenreId in movieCreationData.SubGenreIds) { entityContext.MovieGenreMappingSet.Add(new MovieGenreMapping() { GenreId = subGenreId, MovieId = movie.Id }); } } if (movieCreationData.ActorIds != null) { foreach (var actorId in movieCreationData.ActorIds) { entityContext.MovieActorMappingSet.Add(new MovieActorMapping() { ActorId = actorId, MovieId = movie.Id }); } } //entityContext.SaveChanges(); } return(movieCreationData); }
public static void SeedGenreMovieMappingTable() { var context = new MovieDbContext(); var container = RatedMoviesFixedGenresWithId(); foreach (var genreArray in container) { var movieId = genreArray.Id; foreach (var genreName in genreArray.FixedGenres) { // genreName için genreId int genreId = context.GenreSet.First(t => t.Name == genreName).Id; context.MovieGenreMappingSet.Add( new MovieGenreMapping() { MovieId = movieId, GenreId = genreId, }); context.SaveChanges(); } } }
public MovieCreationData UpdateSec(MovieCreationData movieCreationData) { if (movieCreationData.GenreIds == null) { movieCreationData.GenreIds = new int[] {} } ; if (movieCreationData.SubGenreIds != null && movieCreationData.SubGenreIds.Count() > 0) { movieCreationData.GenreIds = movieCreationData.GenreIds.Union(movieCreationData.SubGenreIds).ToArray(); } using (var entityContext = new MovieDbContext()) { var existingMovie = (from m in entityContext.MovieSet where m.Id == movieCreationData.Id select m).FirstOrDefault(); SimpleMapper.PropertyMap(movieCreationData, existingMovie); entityContext.SaveChanges(); // ADIM 1 // var currentGenreMappings = entityContext.MovieGenreMappingSet.Where(t => t.MovieId == movieCreationData.Id).ToList(); // (True ise) Güncel Movie'de, veritabanındaki haline kıyasla, daha az Genre (Mapping) bulunmakta.. if (currentGenreMappings.Count > movieCreationData.GenreIds.Count()) { for (int i = 0; i < movieCreationData.GenreIds.Count(); i++) { currentGenreMappings[i].GenreId = movieCreationData.GenreIds[i]; } //2 for (int i = movieCreationData.GenreIds.Count(); i < currentGenreMappings.Count; i++) { entityContext.MovieGenreMappingSet.Remove(currentGenreMappings[i]); } entityContext.SaveChanges(); } else { int index = 0; foreach (var currentGenreMap in currentGenreMappings) { currentGenreMap.GenreId = movieCreationData.GenreIds[index]; index++; } if (movieCreationData.GenreIds.Count() != currentGenreMappings.Count) { for (int i = index; i < movieCreationData.GenreIds.Count(); i++) { var newGenreMapping = new MovieGenreMapping() { MovieId = movieCreationData.Id, GenreId = movieCreationData.GenreIds[i] }; entityContext.MovieGenreMappingSet.Add(newGenreMapping); } } entityContext.SaveChanges(); } // ADIM 2 // var currentActorMappings = entityContext.MovieActorMappingSet.Where(t => t.MovieId == movieCreationData.Id).ToList(); // (True ise) Güncel Movie'de, veritabanındaki haline kıyasla, daha az Actor (Mapping) bulunmakta.. if (movieCreationData.ActorIds.Count() < currentActorMappings.Count) { for (int i = 0; i < movieCreationData.ActorIds.Count(); i++) { currentActorMappings[i].ActorId = movieCreationData.ActorIds[i]; } //2 for (int i = movieCreationData.ActorIds.Count(); i < currentActorMappings.Count; i++) { entityContext.MovieActorMappingSet.Remove(currentActorMappings[i]); } entityContext.SaveChanges(); } else { int index = 0; foreach (var currentActorMap in currentActorMappings) { currentActorMap.ActorId = movieCreationData.ActorIds[index]; index++; } if (movieCreationData.ActorIds.Count() != currentActorMappings.Count) { for (int i = index; i < movieCreationData.ActorIds.Count(); i++) { var newActorMapping = new MovieActorMapping() { MovieId = movieCreationData.Id, ActorId = movieCreationData.ActorIds[i] }; entityContext.MovieActorMappingSet.Add(newActorMapping); } } entityContext.SaveChanges(); } } return(movieCreationData); }
public static void SeedActorsTop1000() { var actors = XDocument.Load(@"C:\seed\New_\active.xml"); var actorsName = actors.Elements("Top1000").Elements("Actor").Elements("Name").Select(t => t.Value).ToList(); var actorsConst = actors.Elements("Top1000").Elements("Actor").Elements("Const").Select(t => t.Value).ToList(); var actorsKnownFor = actors.Elements("Top1000").Elements("Actor").Elements("KnownFor").Select(t => t.Value).ToList(); var actorsIsMale = actors.Elements("Top1000").Elements("Actor").Elements("IsMale").Select(t => t.Value).ToList(); var actorsBirthDate = actors.Elements("Top1000").Elements("Actor").Elements("Birth").Select(t => t.Value).ToList(); var forCount = actorsName.Count; var list = new List <string>(); var firstLastNameSeperated = new List <Seeder.FirstLastName>(); foreach (var actorFullName in actorsName) { var splitted = actorFullName.Split(' '); switch (splitted.Count()) { case 1: list.Add(actorFullName); break; case 2: firstLastNameSeperated.Add(new Seeder.FirstLastName() { First = splitted[0], Last = splitted[1] }); break; case 3: firstLastNameSeperated.Add(new Seeder.FirstLastName() { First = splitted[0], Last = splitted[1] + " " + splitted[2] }); break; case 4: firstLastNameSeperated.Add(new Seeder.FirstLastName() { First = splitted[0], Last = splitted[1] + " " + splitted[2] + " " + splitted[3] }); break; } } var birthdateFinal = new List <DateTime>(); foreach (var year in actorsBirthDate) { DateTime dateOut; DateTime.TryParse(year, out dateOut); var dateFinal = dateOut.ToShortDateString(); birthdateFinal.Add(dateOut); } using (var context = new MovieDbContext()) { for (int i = 0; i < forCount; i++) { var actorKnownForMovieId = actorsKnownFor[i]; Movie isThereAmovie = context.MovieSet.FirstOrDefault(t => t.ImdbLink == actorKnownForMovieId); if (isThereAmovie != null) // Film var mı? : Film var. { // Peki aktör var mı? string actorName = actorsName[i]; Actor isThereAnActor = context.ActorSet.FirstOrDefault(t => t.FullName == actorName); if (isThereAnActor == null) // böyle bir aktör yok (map da olamaz). ama bu aktörün (known for) movie'si var. { var actorToDb = new Actor() { Biography = "", BirthDate = birthdateFinal[i], CreatedAt = isThereAmovie.CreatedAt, DeathDate = null, FirstName = firstLastNameSeperated[i].First, LastName = firstLastNameSeperated[i].Last, FullName = actorsName[i], Gender = Convert.ToBoolean(actorsIsMale[i]), CountryId = 1, CountyId = 1, StateId = 1, ImdbLink = actorsConst[i], Photo = "null" }; context.ActorSet.Add(actorToDb); context.SaveChanges(); context.MovieActorMappingSet.Add(new MovieActorMapping() { MovieId = isThereAmovie.Id, ActorId = actorToDb.Id }); context.SaveChanges(); } else { // böyle bir aktör var. Film de vardı. O zaman map yapma zamanı. // Tabii hali hazırda map yoksa... // önce gender ayarı MovieActorMapping mp = context.MovieActorMappingSet .FirstOrDefault(t => t.MovieId == isThereAmovie.Id && t.ActorId == isThereAnActor.Id); if (mp == null) // map yok { context.MovieActorMappingSet.Add(new MovieActorMapping() { MovieId = isThereAmovie.Id, ActorId = isThereAnActor.Id }); context.SaveChanges(); } } } else { // Actor için KnownFor FİLM YOK. Peki böyle bir Actor var mı? string actorName = actorsName[i]; Actor isThereAnActor = context.ActorSet.FirstOrDefault(t => t.FullName == actorName); if (isThereAnActor == null) // böyle bir aktör de YOK: ekle o zaman. { var actorToDb = new Actor() { Biography = "", BirthDate = birthdateFinal[i], CreatedAt = DateTime.Now, DeathDate = null, FirstName = firstLastNameSeperated[i].First, LastName = firstLastNameSeperated[i].Last, FullName = actorsName[i], Gender = Convert.ToBoolean(actorsIsMale[i]), CountryId = 1, CountyId = 1, StateId = 1, ImdbLink = actorsConst[i], Photo = "null" }; context.ActorSet.Add(actorToDb); context.SaveChanges(); } } } } }