// Upload een foto en store hem lokaal, zet een referentie naar de foto in de database
        public string UploadPhoto(string filename, byte[] imageStream, long userId)
        {
            string filePath = Path.Combine((Environment.GetFolderPath(Environment.SpecialFolder.Desktop)), filename); // Host HostingEnvironment.MapPath
            int    length   = 0;
            Stream stream   = new MemoryStream(imageStream);

            stream.Position = 0;

            using (FileStream writer = new FileStream(filePath, FileMode.Create))
            {
                int readCount;
                var buffer = new byte[8192];
                while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    writer.Write(buffer, 0, readCount);
                    length += readCount;
                }
            }
            using (ThreadingEntities ent = new ThreadingEntities())
            {
                Photo foto = new Photo();
                foto.PhotoName = filename;
                foto.Path      = filePath;
                foto.UserId    = userId;
                foto.ImageData = imageStream;

                ent.Photos.Attach(foto);
                ent.Photos.Add(foto);
                ent.SaveChanges();
            }

            return("Succes");
        }
 // Delete een foto uit de database
 public void DeletePhoto(long photoId)
 {
     using (ThreadingEntities ent = new ThreadingEntities())
     {
         var removeFoto = (from f in ent.Photos where f.PhotoId == photoId select f).First();
         ent.Photos.Remove(removeFoto);
         ent.SaveChanges();
     }
 }
 // Voeg een nieuwe gebruiker toe aan de database
 public void AddUser(User user)
 {
     using (ThreadingEntities ent = new ThreadingEntities())
     {
         ent.Users.Attach(user);
         ent.Users.Add(user);
         ent.SaveChanges();
     }
 }
 // Set het wachtwoord van de gebruiker in de database aan de hand van de gebruikersID
 public bool SetUserPassword(string pw, long userId)
 {
     using (ThreadingEntities ent = new ThreadingEntities())
     {
         var user = (from g in ent.Users where g.UserId == userId select g).First();
         if (user != null)
         {
             user.Password = pw;
             ent.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
 // Verander de gebruikersnaam in de database aan de hand van gebruikersID
 public bool SetUsername(string gebrNaam, long userId)
 {
     using (ThreadingEntities ent = new ThreadingEntities())
     {
         var user = (from g in ent.Users where g.UserId == userId select g).First();
         if (user != null)
         {
             user.Username = gebrNaam;
             ent.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
 // Geef de foto een nieuwe naam aan de hand van fotoID
 public bool SetPhotoName(long photoId, string naam)
 {
     using (ThreadingEntities ent = new ThreadingEntities())
     {
         var foto = (from f in ent.Photos where f.PhotoId == photoId select f).First();
         if (foto != null)
         {
             foto.PhotoName = naam;
             ent.SaveChanges();
             return(true);
         }
         return(true);
     }
 }