public bool CreateDog(DogViewModel dogToBeCreated, string userId, HttpPostedFileBase uploadPhoto) { try { Dog newDog = new Dog() { DogOwnerID = userId, DogName = dogToBeCreated.DogName, DogSize = dogToBeCreated.DogSize, Description = dogToBeCreated.Description }; dogRepo.Insert(newDog); unitOfWork.Commit(); DogPhoto newDogPhoto = new DogPhoto() { DogID = newDog.DogID, Photo = Image.FromStream(uploadPhoto.InputStream).ResizeImageConvertInBytes(360, 270) }; dogPhotoRepo.Insert(newDogPhoto); unitOfWork.Commit(); return(true); } catch (EntityException ex) { Debug.WriteLine(ex.Message); return(false); } finally { if (unitOfWork != null) { unitOfWork.Dispose(); } } }
internal static void RandomDogsPerDogOwner(this LendADogDemoDb context, int dogsPerOwner, int photosPerDog) { #region Helpers List <DogOwner> dogOwners = context.DogOwners.ToList(); var currentDirName = @"C:\Users\karco\Desktop\DogPhotos"; var aveablePhotos = Directory.GetFiles(currentDirName); byte[][] ImagesBytes = new byte[aveablePhotos.Length][]; for (int i = 0; i < aveablePhotos.Length; i++) { using (Image img = Image.FromFile(aveablePhotos[i])) { var imgBiteNew = img.ResizeImageConvertInBytes(360, 270); ImagesBytes[i] = imgBiteNew; } } string[] dogNames = new string[] { "Rufus", "Ella", "Luke", "Dixie", "Kona", "Nala", "Penelope", "Jasmine", "Lola", "Cisco", "Maddie", "Princess", "Blue", "Payton", "Scooter", "Marley", "Buddy", "Gizmo", "Brutus", "Moose", "Tiger", "Teddy", "Rex", "Cocoa", "Lacey", "Coco", "Rocky", "Hank", "Rosie", "Chance", "Bubba", "Benji", "Ace", "Zara", "Scout", "Gigi", "Tucker", "Baxter", "Gunner", "Bruno" }; string[] firstWord = new string[] { "active", "adorable", "adventurous", "amazing", "bright", "charming", "cheerful", "clever", "delightful", "energetic", "intelligent", "interesting", "loving", "playful", "positive", "sensitive", "friendly" }; #endregion using (TransactionScope scope = new TransactionScope()) { using (context = new LendADogDemoDb()) { int a = 0; foreach (DogOwner user in dogOwners) { a++; for (int i = 0; i < dogsPerOwner; i++) { Dog newDog = new Dog() { DogName = dogNames[random.Next(dogNames.Length)], DogSize = (Size)random.Next(1, 4), DogOwnerID = user.Id, Description = $" is {firstWord[random.Next(firstWord.Length)]} dog. Perfect guest for your home.", DogPhotos = new List <DogPhoto>() }; for (int b = 0; b < photosPerDog; b++) { DogPhoto newDogPhoto = new DogPhoto() { DogID = newDog.DogID, Photo = ImagesBytes[random.Next(ImagesBytes.Length)] }; newDog.DogPhotos.Add(newDogPhoto); } context.Dogs.Add(newDog); } if (a % 10 == 0) { context.SaveChanges(); context.Dispose(); context = new LendADogDemoDb(); } } context.SaveChanges(); } scope.Complete(); } }