private static void SaveToDb(IEnumerable<Picture> pictures, int styleId) { using (var context = new PicturesDbContext()) { var existPictureNames = context.Pictures.Where(i => i.StyleId == styleId).Select(i => i.ImageUrl).ToDictionary(i => i); var existPainters = context.Painters.Select(i => new {i.Name, i.Id}).ToDictionary(i => i.Name,i => i.Id); foreach (var parsedPicture in pictures) { if (!existPictureNames.Keys.Contains(parsedPicture.Url)) { long painterId; GuessThePicture.Data.Picture newPicture = new GuessThePicture.Data.Picture() { Name = parsedPicture.Name, ImageUrl = parsedPicture.Url, Guid = Guid.NewGuid().ToString(), StyleId = styleId }; if (existPainters.TryGetValue(parsedPicture.Painter, out painterId)) { newPicture.PainterId = painterId; } else { var newPainter = new Painter() { Name = parsedPicture.Painter, }; existPainters.Add(parsedPicture.Painter,0); newPicture.Painter = newPainter; context.Painters.Add(newPainter); } context.Pictures.Add(newPicture); } } context.SaveChanges(); } }
public void UpdateExternalLoginValues(UserDto userDto, string firstName, string lastName, string photoRec) { using (var context = new PicturesDbContext()) { var userEntity = new User {Id = userDto.Id}; var entity = context.Users.Attach(userEntity); var wasModified = false; if (userDto.PhotoRec != photoRec) { entity.PhotoRec = photoRec; //context.Entry(entity).Property((item) => item.PhotoRec).IsModified = true; wasModified = true; } if (userDto.FirstName != firstName) { entity.FirstName = photoRec; //context.Entry(entity).Property((item) => item.FirstName).IsModified = true; wasModified = true; } if (userDto.LastName != lastName) { entity.LastName = lastName; //context.Entry(entity).Property((item) => item.LastName).IsModified = true; } if (wasModified) context.SaveChanges(); } }