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();
            }
        }