public int Add(Conversation conversation)
 {
     using (var context = new ChatContext())
     {
         try
         {
             /// User remapping is done here because EF didn't recognize conversation users as
             /// same object as ones in the context so it created duplicate entries.
             /// Atm I had no time to find a better solution.
             Conversation newConversation = new Conversation();
             foreach (var participant in conversation.Participants)
             {
                 var user = context.Users.Find(participant.Id);
                 if (user != null)
                     newConversation.Participants.Add(user);
             }
             context.Conversations.Add(newConversation);
             context.SaveChanges();
             return newConversation.Id;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return -1;
         }
     }
 }
        public bool Remove(User user)
        {
            using (var context = new ChatContext())
            {
                try
                {
                    var credential = context.Credentials.ToList().First(c => c.User.Username.Equals(user.Username));
                    context.Credentials.Remove(credential);
                    context.SaveChanges();

                    var fetchedUser = context.Users.First(u => u.Username.Equals(user.Username));
                    context.Users.Remove(fetchedUser);
                    context.SaveChanges();

                    return true;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                    return false;
                }
            }
        }
 public bool Remove(int messageId)
 {
     using (var context = new ChatContext())
     {
         try
         {
             context.Messages.Remove(context.Messages.Find(messageId));
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public bool Add(Message message)
 {
     using (var context = new ChatContext())
     {
         try
         {
             context.Messages.Add(message);
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public bool Update(Conversation conversation)
 {
     using (var context = new ChatContext())
     {
         try
         {
             context.Conversations.Remove(context.Conversations.Find(conversation.Id));
             context.Conversations.Add(conversation);
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public bool Add(User user, string password)
 {
     Credential credential = new Credential();
     credential.User = user;
     credential.Password = password;
     using (var context = new ChatContext())
     {
         try
         {
             context.Users.Add(user);
             context.Credentials.Add(credential);
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
        public bool Update(User user)
        {
            using (var context = new ChatContext())
            {
                try
                {
                    var originalUser = context.Users.Single(u => u.Username.Equals(user.Username));
                    originalUser.Conversations = user.Conversations;

                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                    return false;
                }
            }
        }