public bool Upvote(int id)
 {
     //Upvote an animal
       using (var context = new WildscreenAnimalsAPI_dbEntities())
       {
     var image = context.AnimalImages.Where(x => x.Id == id).FirstOrDefault();
     image.Upvotes++;
     context.SaveChanges();
       }
       return true;
 }
        // GET api/Animals
        public IEnumerable<dynamic> GetRandom(int count = 10)
        {
            var animals = new List<Animal>();
              var animalImages = new List<AnimalImage>();
              using(var context = new WildscreenAnimalsAPI_dbEntities())
              {
            animals = context.Animals.ToList();
            animalImages = context.AnimalImages.ToList();
              }
              //take data and take random selection
              var animalData = animalImages.Select(x => new { Image = x, Animal = animals.Where(a => a.Id == x.Animal_Id).FirstOrDefault() }).OrderBy(elem => Guid.NewGuid()).Take(count);

              return animalData;
        }
        public ActionResult Index()
        {
            ViewBag.Title = "Best Images";

              var animals = new List<Animal>();
              var animalImages = new List<AnimalImage>();

              using (var context = new WildscreenAnimalsAPI_dbEntities())
              {
            animals = context.Animals.ToList();
            animalImages = context.AnimalImages.Where(x => x.Upvotes > 0 && x.Upvotes > x.Downvotes).OrderByDescending(x => x.Upvotes).Take(10).ToList();
              }

              //take data and take random selection
              var animalData = animalImages.Select(x => new { Image = x, Animal = animals.Where(a => a.Id == x.Animal_Id).FirstOrDefault() }).OrderByDescending(x => x.Image.Upvotes).Take(10);

              return View(animalData);
        }
        static void Main(string[] args)
        {
            //open DB
              using (var context = new WildscreenAnimalsAPI_dbEntities())
              {
            var animals = context.Animals.ToList();//Get all animals
            var allAnimalImages = context.AnimalImages.ToList();

            foreach (var animal in animals)
            {
              //Do a flickr search on the animal
              var json = SendRequest(string.Format(flickrSearchUrl, flickrKey, WebUtility.UrlEncode(animal.Name)));
              var searchResult = JsonConvert.DeserializeObject<FlickrSearchResponse>(json);
              //Loop through flikr images returned in the resuts and get image urls for them
              if(searchResult.photos != null && searchResult.photos.photo.Count > 0)
              {
            foreach(var fPhoto in searchResult.photos.photo)
            {
              var photo = new AnimalImage()
              {
                Animal_Id = animal.Id,
                CreatedOn = DateTime.Now,
                Downvotes = 0,
                Upvotes = 0,
                SourceSystem = "Flickr",
                ImageUrl = string.Format(flickrImageUrl,fPhoto.farm,fPhoto.server,fPhoto.id,fPhoto.secret),
                Name = fPhoto.title
              };
              if(!allAnimalImages.Any(x => x.ImageUrl == photo.ImageUrl))
                context.AnimalImages.Add(photo);
            }//foreach
              }//if
              //Create an image record
            }
            //Save
            context.SaveChanges();
              } //close db
        }