public override IList<Group> GetAllGroups(string user)
 {
     var context = new FriendsEntities();
     var list =
         context.tblFriends_Groups.Where(item => item.CreatedBy == user).OrderBy(item => item.Name).ToList();
     return Convert(list);
 }
 public override IList<Friend> GetFriends(string user)
 {
     var context = new FriendsEntities();
     var list =
         context.tblFriends_Friends.Include("Groups").Where(item => item.CreatedBy == user).OrderBy(
             item => item.Letter).ToList();
     return Convert(list);
 }
 public override void DeleteGroup(int id)
 {
     var context = new FriendsEntities();
     var obj = context.tblFriends_Groups.Include("Friends").Where(item => item.Id == id).FirstOrDefault();
     if (obj != null)
     {
         obj.Friends.Clear();
         context.DeleteObject(obj);
         context.SaveChanges();
     }
 }
        public override int InsertFriend(Friend friend)
        {
            var context = new FriendsEntities();
            var obj = new tblFriends_Friends();
            context.AddTotblFriends_Friends(obj);
            friend.CopyToObject(obj);

            if (friend.Groups != null)
            {
                foreach (var group in friend.Groups)
                {
                    var item = new tblFriends_Groups {Id = group.Id};
                    context.AttachTo("tblFriends_Groups", item);
                    obj.Groups.Add(item);
                }
            }

            context.SaveChanges();
            return obj.Id;
        }
        public override void UpdateFriend(Friend friend)
        {
            var context = new FriendsEntities();
            var obj = context.tblFriends_Friends.Include("Groups").Where(item => item.Id == friend.Id).FirstOrDefault();
            if (obj != null)
            {
                obj.Groups.Clear();
                friend.CopyToObject(obj);
                if (friend.Groups.Count > 0)
                {
                    var groups =
                        context.tblFriends_Groups.Where(
                            Utils.BuildContainsExpression<tblFriends_Groups, int>(item => item.Id,
                                                                                  friend.Groups.Select(item => item.Id)));
                    foreach (var group in groups)
                    {
                        obj.Groups.Add(group);
                    }
                }

                context.SaveChanges();
            }
        }
 public override int InsertGroup(Group group)
 {
     var context = new FriendsEntities();
     var obj = new tblFriends_Groups();
     group.CopyToObject(obj);
     context.AddTotblFriends_Groups(obj);
     context.SaveChanges();
     return obj.Id;
 }
 public override Group GetGroupById(int id)
 {
     var context = new FriendsEntities();
     var obj = context.tblFriends_Groups.Where(item => item.Id == id).FirstOrDefault();
     return Convert(obj);
 }
 public override void UpdateGroup(Group group)
 {
     var context = new FriendsEntities();
     var obj = new tblFriends_Groups {Id = group.Id};
     context.AttachTo("tblFriends_Groups", obj);
     group.CopyToObject(obj);
     context.SaveChanges();
 }