public void RemoveUser(string username)
 {
     using (TorrentDBEntities db = new TorrentDBEntities())
     {
         Users user = db.Users.Find(username);
         db.Users.Remove(user);
         db.SaveChanges();
     }
 }
 public void AddUser(User user)
 {
     using (TorrentDBEntities db = new TorrentDBEntities())
     {
         Users dbUser = JsonConvert.DeserializeObject <Users>(JsonConvert.SerializeObject(user));
         db.Users.Add(dbUser);
         db.SaveChanges();
     }
 }
 public void AddFilesByUser(List <File> files)
 {
     using (TorrentDBEntities db = new TorrentDBEntities())
     {
         foreach (File file in files)
         {
             Files dbFile = JsonConvert.DeserializeObject <Files>(JsonConvert.SerializeObject(file));
             db.Files.Add(dbFile);
         }
         db.SaveChanges();
     }
 }
 public void RemoveFilesByUserName(string username)
 {
     using (TorrentDBEntities db = new TorrentDBEntities())
     {
         foreach (Files file in db.Files)
         {
             if (file.Username.Equals(username))
             {
                 db.Files.Remove(file);
             }
         }
         db.SaveChanges();
     }
 }
        public void ClearAll()
        {
            using (TorrentDBEntities db = new TorrentDBEntities())
            {
                foreach (Users user in db.Users)
                {
                    db.Users.Remove(user);
                }

                foreach (Files file in db.Files)
                {
                    db.Files.Remove(file);
                }

                db.SaveChanges();
            }
        }
        public void UpdateUser(User updatedUser, string existingUsername)
        {
            using (TorrentDBEntities db = new TorrentDBEntities())
            {
                var receivedUser = db.Users.Find(existingUsername);
                //var receivedUser = (from user in db.Users
                //                   where user.UserName.Equals(updatedUser.UserName)
                //                   select user).Single();
                receivedUser.UserName  = updatedUser.UserName;
                receivedUser.Password  = updatedUser.Password;
                receivedUser.Enabled   = updatedUser.Enabled;
                receivedUser.Connected = updatedUser.Connected;
                receivedUser.IP        = updatedUser.IP;
                receivedUser.Port      = updatedUser.Port;

                db.SaveChanges();
            }
        }
 public void AddFile()
 {
     using (TorrentDBEntities db = new TorrentDBEntities())
     {
         try
         {
             Files file = new Files();
             file.Id       = 1;
             file.FileName = "filename";
             file.Size     = 123;
             db.Files.Add(file);
             db.SaveChanges();
         }
         catch (Exception e)
         {
             Console.WriteLine("Exception = " + e.StackTrace);
         }
     }
 }