public PersonalityType GetPersonalityType(int id)
 {
     using (var db = new RevolutionEntities1())
       {
     return db.PersonalityTypes.FirstOrDefault(pt => pt.ID == id);
       }
 }
 public List<PersonalityType> GetAllPersonalityTypes()
 {
     using (var db = new RevolutionEntities1())
       {
     return db.PersonalityTypes.ToList();
       }
 }
 public Character GetCharacter(int id)
 {
     using (var db = new RevolutionEntities1())
       {
     var ch = db.Characters.Where(c => c.ID == id && !c.Deleted).FirstOrDefault();
         return ch;
       }
 }
 public Phrase GetPhrase(int id)
 {
     using (var db = new RevolutionEntities1())
       {
     var phrase = db.Phrases.Where(p => p.ID == id).FirstOrDefault();
     return new Phrase { ID = phrase.ID, Immediacy = phrase.Immediacy, IsQuestion = phrase.IsQuestion, IsSpontaneous = phrase.IsSpontaneous, PersonalityPhrases = phrase.PersonalityPhrases, Text = phrase.Text, Topic = phrase.Topic, TopicID = phrase.TopicID };
       }
 }
        public List<Character> GetAllCharacters()
        {
            using (var db = new RevolutionEntities1())
            {
                var chars = db.Characters.Select(ch => ch as Character).ToList();

                return chars;
            }
        }
 public Phrase SavePhrase(Phrase phrase)
 {
     using (var db = new RevolutionEntities1())
       {
     db.Phrases.Add(phrase);
     db.SaveChanges();
     return phrase;
       }
 }
 public void RemoveCharacter(int id)
 {
     using (var db = new RevolutionEntities1())
       {
     var character = db.Characters.FirstOrDefault(c => c.ID == id);
     if (character == null) return;
     character.Deleted = true;
     db.SaveChanges();
       }
 }
 public void RemovePhrase(int id)
 {
     using (var db = new RevolutionEntities1())
       {
     var phrase = db.Phrases.FirstOrDefault(c => c.ID == id);
     if (phrase == null) return;
     db.Phrases.Remove(phrase);
     db.SaveChanges();
       }
 }
        public List<Character> GetCharacters(XElement query)
        {
            var serializer = new ExpressionSerializer();
              var expression = serializer.Deserialize<Func<Character, bool>>(query);
              if (expression == null)
            return null;

              using (var db = new RevolutionEntities1())
              {
            return db.Characters.Where(expression).ToList();
              }
        }
 public List<Phrase> GetPhrasesByTopic(int topicId)
 {
     using (var db = new RevolutionEntities1())
       {
     return db.Phrases.Where(p => p.TopicID == topicId).Select(p => new Phrase
       { ID = p.ID,
     Immediacy = p.Immediacy,
     IsQuestion = p.IsQuestion,
     IsSpontaneous = p.IsSpontaneous,
     PersonalityPhrases = p.PersonalityPhrases,
     Text = p.Text, Topic = p.Topic,
     TopicID = p.TopicID
       }).ToList();
       }
 }
        public Character SaveCharacter(Character character)
        {
            using (var db = new RevolutionEntities1())
            {
                Character ch = db.Characters.Where(c => c.ID == character.ID).FirstOrDefault();

                if (ch != default(Character))
                {
                    db.Entry(ch).CurrentValues.SetValues(character);
                }
                else
                {
                    db.Characters.Add(character);
                }
                db.SaveChanges();
                return character;
            }
        }