/// <summary>
 /// Merge changes inside entity <paramref name="from" /> to the entity <paramref name="to" />. Any changes in <paramref name="from" /> that is not changed in <paramref name="to" /> is updated inside <paramref name="to" />.
 /// </summary>
 /// <param name="from">The "old" entity acting as merging source.</param>
 /// <param name="to">The "new" entity which inherits changes made in <paramref name="from" />.</param>
 /// <returns>
 /// </returns>
 public static void MergeChanges(UserAssociation from, UserAssociation to)
 {
     if (to.IsPersisted)
     {
         if (from.IsAssocCountModified && !to.IsAssocCountModified)
         {
             to.AssocCount           = from.AssocCount;
             to.IsAssocCountModified = true;
         }
         if (from.IsAssocMemoModified && !to.IsAssocMemoModified)
         {
             to.AssocMemo           = from.AssocMemo;
             to.IsAssocMemoModified = true;
         }
         if (from.IsInteractCountModified && !to.IsInteractCountModified)
         {
             to.InteractCount           = from.InteractCount;
             to.IsInteractCountModified = true;
         }
         if (from.IsLastAssocModified && !to.IsLastAssocModified)
         {
             to.LastAssoc           = from.LastAssoc;
             to.IsLastAssocModified = true;
         }
         if (from.IsLastInteractModified && !to.IsLastInteractModified)
         {
             to.LastInteract           = from.LastInteract;
             to.IsLastInteractModified = true;
         }
         if (from.IsVotesModified && !to.IsVotesModified)
         {
             to.Votes           = from.Votes;
             to.IsVotesModified = true;
         }
     }
     else
     {
         to.IsPersisted             = from.IsPersisted;
         to.FromUserID              = from.FromUserID;
         to.ToUserID                = from.ToUserID;
         to.TypeID                  = from.TypeID;
         to.CreateDate              = from.CreateDate;
         to.AssocCount              = from.AssocCount;
         to.IsAssocCountModified    = from.IsAssocCountModified;
         to.AssocMemo               = from.AssocMemo;
         to.IsAssocMemoModified     = from.IsAssocMemoModified;
         to.InteractCount           = from.InteractCount;
         to.IsInteractCountModified = from.IsInteractCountModified;
         to.LastAssoc               = from.LastAssoc;
         to.IsLastAssocModified     = from.IsLastAssocModified;
         to.LastInteract            = from.LastInteract;
         to.IsLastInteractModified  = from.IsLastInteractModified;
         to.Votes           = from.Votes;
         to.IsVotesModified = from.IsVotesModified;
     }
 }
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) intrinsic identifiers.
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityTheSame(UserAssociation other)
 {
     if (other == null)
     {
         return(false);
     }
     else
     {
         return(FromUserID == other.FromUserID && ToUserID == other.ToUserID && TypeID == other.TypeID);
     }
 }
        /// <summary>
        /// Update changes to the current entity compared to an input <paramref name="newdata" /> and set the entity to a proper state for updating.
        /// </summary>
        /// <param name="newdata">The "new" entity acting as the source of the changes, if any.</param>
        /// <returns>
        /// </returns>
        public void UpdateChanges(UserAssociation newdata)
        {
            int cnt = 0;

            if (AssocMemo != newdata.AssocMemo)
            {
                AssocMemo           = newdata.AssocMemo;
                IsAssocMemoModified = true;
                cnt++;
            }
            IsEntityChanged = cnt > 0;
        }
        /// <summary>
        /// Internal use
        /// </summary>
        public UserAssociation ShallowCopy(bool allData = false)
        {
            UserAssociation e = new UserAssociation();

            e.IsInitializing  = true;
            e.FromUserID      = FromUserID;
            e.ToUserID        = ToUserID;
            e.TypeID          = TypeID;
            e.CreateDate      = CreateDate;
            e.AssocMemo       = AssocMemo;
            e.DistinctString  = GetDistinctString(true);
            e.IsPersisted     = true;
            e.IsEntityChanged = false;
            e.IsInitializing  = false;
            return(e);
        }
        /// <summary>
        /// Update changes to the current entity compared to an input <paramref name="newdata" /> and set the entity to a proper state for updating.
        /// </summary>
        /// <param name="newdata">The "new" entity acting as the source of the changes, if any.</param>
        /// <returns>
        /// </returns>
        public void UpdateChanges(UserAssociation newdata)
        {
            int cnt = 0;

            if (AssocCount != newdata.AssocCount)
            {
                AssocCount           = newdata.AssocCount;
                IsAssocCountModified = true;
                cnt++;
            }
            if (AssocMemo != newdata.AssocMemo)
            {
                AssocMemo           = newdata.AssocMemo;
                IsAssocMemoModified = true;
                cnt++;
            }
            if (InteractCount != newdata.InteractCount)
            {
                InteractCount           = newdata.InteractCount;
                IsInteractCountModified = true;
                cnt++;
            }
            if (LastAssoc != newdata.LastAssoc)
            {
                LastAssoc           = newdata.LastAssoc;
                IsLastAssocModified = true;
                cnt++;
            }
            if (LastInteract != newdata.LastInteract)
            {
                LastInteract           = newdata.LastInteract;
                IsLastInteractModified = true;
                cnt++;
            }
            if (Votes != newdata.Votes)
            {
                Votes           = newdata.Votes;
                IsVotesModified = true;
                cnt++;
            }
            IsEntityChanged = cnt > 0;
        }
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) primary key(s).
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityIdentical(UserAssociation other)
 {
     if (other == null)
     {
         return(false);
     }
     if (FromUserID != other.FromUserID)
     {
         return(false);
     }
     if (ToUserID != other.ToUserID)
     {
         return(false);
     }
     if (TypeID != other.TypeID)
     {
         return(false);
     }
     return(true);
 }
 /// <summary>
 /// Merge changes inside entity <paramref name="from" /> to the entity <paramref name="to" />. Any changes in <paramref name="from" /> that is not changed in <paramref name="to" /> is updated inside <paramref name="to" />.
 /// </summary>
 /// <param name="from">The "old" entity acting as merging source.</param>
 /// <param name="to">The "new" entity which inherits changes made in <paramref name="from" />.</param>
 /// <returns>
 /// </returns>
 public static void MergeChanges(UserAssociation from, UserAssociation to)
 {
     if (to.IsPersisted)
     {
         if (from.IsAssocMemoModified && !to.IsAssocMemoModified)
         {
             to.AssocMemo           = from.AssocMemo;
             to.IsAssocMemoModified = true;
         }
     }
     else
     {
         to.IsPersisted         = from.IsPersisted;
         to.FromUserID          = from.FromUserID;
         to.ToUserID            = from.ToUserID;
         to.TypeID              = from.TypeID;
         to.CreateDate          = from.CreateDate;
         to.AssocMemo           = from.AssocMemo;
         to.IsAssocMemoModified = from.IsAssocMemoModified;
     }
 }
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) intrinsic identifiers.
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityTheSame(UserAssociation other)
 {
     if (other == null)
         return false;
     else
         return FromUserID == other.FromUserID &&  ToUserID == other.ToUserID &&  TypeID == other.TypeID;
 }              
 /// <summary>
 /// Merge changes inside entity <paramref name="from" /> to the entity <paramref name="to" />. Any changes in <paramref name="from" /> that is not changed in <paramref name="to" /> is updated inside <paramref name="to" />.
 /// </summary>
 /// <param name="from">The "old" entity acting as merging source.</param>
 /// <param name="to">The "new" entity which inherits changes made in <paramref name="from" />.</param>
 /// <returns>
 /// </returns>
 public static void MergeChanges(UserAssociation from, UserAssociation to)
 {
     if (to.IsPersisted)
     {
         if (from.IsAssocCountModified && !to.IsAssocCountModified)
         {
             to.AssocCount = from.AssocCount;
             to.IsAssocCountModified = true;
         }
         if (from.IsAssocMemoModified && !to.IsAssocMemoModified)
         {
             to.AssocMemo = from.AssocMemo;
             to.IsAssocMemoModified = true;
         }
         if (from.IsDoNotNotifyModified && !to.IsDoNotNotifyModified)
         {
             to.DoNotNotify = from.DoNotNotify;
             to.IsDoNotNotifyModified = true;
         }
         if (from.IsInteractCountModified && !to.IsInteractCountModified)
         {
             to.InteractCount = from.InteractCount;
             to.IsInteractCountModified = true;
         }
         if (from.IsLastAssocModified && !to.IsLastAssocModified)
         {
             to.LastAssoc = from.LastAssoc;
             to.IsLastAssocModified = true;
         }
         if (from.IsLastInteractModified && !to.IsLastInteractModified)
         {
             to.LastInteract = from.LastInteract;
             to.IsLastInteractModified = true;
         }
         if (from.IsNoMessagesModified && !to.IsNoMessagesModified)
         {
             to.NoMessages = from.NoMessages;
             to.IsNoMessagesModified = true;
         }
         if (from.IsNotifyButBlockModified && !to.IsNotifyButBlockModified)
         {
             to.NotifyButBlock = from.NotifyButBlock;
             to.IsNotifyButBlockModified = true;
         }
         if (from.IsVotesModified && !to.IsVotesModified)
         {
             to.Votes = from.Votes;
             to.IsVotesModified = true;
         }
     }
     else
     {
         to.IsPersisted = from.IsPersisted;
         to.FromUserID = from.FromUserID;
         to.ToUserID = from.ToUserID;
         to.TypeID = from.TypeID;
         to.CreateDate = from.CreateDate;
         to.AssocCount = from.AssocCount;
         to.IsAssocCountModified = from.IsAssocCountModified;
         to.AssocMemo = from.AssocMemo;
         to.IsAssocMemoModified = from.IsAssocMemoModified;
         to.DoNotNotify = from.DoNotNotify;
         to.IsDoNotNotifyModified = from.IsDoNotNotifyModified;
         to.InteractCount = from.InteractCount;
         to.IsInteractCountModified = from.IsInteractCountModified;
         to.LastAssoc = from.LastAssoc;
         to.IsLastAssocModified = from.IsLastAssocModified;
         to.LastInteract = from.LastInteract;
         to.IsLastInteractModified = from.IsLastInteractModified;
         to.NoMessages = from.NoMessages;
         to.IsNoMessagesModified = from.IsNoMessagesModified;
         to.NotifyButBlock = from.NotifyButBlock;
         to.IsNotifyButBlockModified = from.IsNotifyButBlockModified;
         to.Votes = from.Votes;
         to.IsVotesModified = from.IsVotesModified;
     }
 }
 /// <summary>
 /// Internal use
 /// </summary>
 public UserAssociation ShallowCopy(bool allData = false, bool preserveState = false)
 {
     UserAssociation e = new UserAssociation();
     e.StartAutoUpdating = false;
     e.FromUserID = FromUserID;
     e.ToUserID = ToUserID;
     e.TypeID = TypeID;
     e.CreateDate = CreateDate;
     e.AssocCount = AssocCount;
     if (preserveState)
         e.IsAssocCountModified = IsAssocCountModified;
     else
         e.IsAssocCountModified = false;
     e.DoNotNotify = DoNotNotify;
     if (preserveState)
         e.IsDoNotNotifyModified = IsDoNotNotifyModified;
     else
         e.IsDoNotNotifyModified = false;
     e.InteractCount = InteractCount;
     if (preserveState)
         e.IsInteractCountModified = IsInteractCountModified;
     else
         e.IsInteractCountModified = false;
     e.LastAssoc = LastAssoc;
     if (preserveState)
         e.IsLastAssocModified = IsLastAssocModified;
     else
         e.IsLastAssocModified = false;
     e.LastInteract = LastInteract;
     if (preserveState)
         e.IsLastInteractModified = IsLastInteractModified;
     else
         e.IsLastInteractModified = false;
     e.NoMessages = NoMessages;
     if (preserveState)
         e.IsNoMessagesModified = IsNoMessagesModified;
     else
         e.IsNoMessagesModified = false;
     e.NotifyButBlock = NotifyButBlock;
     if (preserveState)
         e.IsNotifyButBlockModified = IsNotifyButBlockModified;
     else
         e.IsNotifyButBlockModified = false;
     e.Votes = Votes;
     if (preserveState)
         e.IsVotesModified = IsVotesModified;
     else
         e.IsVotesModified = false;
     if (allData)
     {
         e.AssocMemo = AssocMemo;
         if (preserveState)
             e.IsAssocMemoModified = IsAssocMemoModified;
         else
             e.IsAssocMemoModified = false;
     }
     e.DistinctString = GetDistinctString(true);
     e.IsPersisted = IsPersisted;
     if (preserveState)
         e.IsEntityChanged = IsEntityChanged;
     else
         e.IsEntityChanged = false;
     e.StartAutoUpdating = true;
     return e;
 }
 /// <summary>
 /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) primary key(s).
 /// </summary>
 /// <param name="other">The entity to be compared to.</param>
 /// <returns>
 ///   The result of comparison.
 /// </returns>
 public bool IsEntityIdentical(UserAssociation other)
 {
     if (other == null)
         return false;
     if (FromUserID != other.FromUserID)
         return false;
     if (ToUserID != other.ToUserID)
         return false;
     if (TypeID != other.TypeID)
         return false;
     return true;
 }              
 public static async Task LeaveUserMessage(string chatHubId, string userId, string peerId, string replyId, string message)
 {
     var cntx = Cntx;
     UserServiceProxy usvc = new UserServiceProxy();
     var u = await usvc.LoadEntityByKeyAsync(cntx, userId);
     var peer = await usvc.LoadEntityByKeyAsync(cntx, peerId);
     ShortMessageServiceProxy msvc = new ShortMessageServiceProxy();
     PeerShotMessage m = new PeerShotMessage();
     var now = DateTime.UtcNow;
     ShortMessage msg = new ShortMessage
     {
         ID = Guid.NewGuid().ToString(),
         ApplicationID = AppId,
         TypeID = 1,
         GroupID = null,
         FromID = userId,
         ToID = peerId,
         ReplyToID = string.IsNullOrEmpty(replyId) ? null : replyId,
         CreatedDate = now,
         LastModified = now,
         MsgText = message,
         IsNotReceived = true
     };
     await msvc.AddOrUpdateEntitiesAsync(cntx, new ShortMessageSet(), new ShortMessage[] { msg });
     UserAssociationServiceProxy uasvc = new UserAssociationServiceProxy();
     DateTime dt = DateTime.UtcNow;
     List<UserAssociation> lass = new List<UserAssociation>();
     UserAssociation utop = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);
     if (utop == null)
     {
         utop = new UserAssociation
         {
             TypeID = ApplicationContext.ChatAssocTypeId,
             FromUserID = userId,
             ToUserID = peerId,
             CreateDate = dt,
             AssocCount = 0,
             LastAssoc = dt,
             InteractCount = 1,
             Votes = 0
         };
     }
     else
         utop.InteractCount++;
     lass.Add(utop);
     if (!string.IsNullOrEmpty(replyId))
     {
         UserAssociation ptou = await uasvc.LoadEntityByKeyAsync(cntx, peerId, userId, ApplicationContext.ChatAssocTypeId);
         if (ptou == null)
         {
             ptou = new UserAssociation
             {
                 TypeID = ApplicationContext.ChatAssocTypeId,
                 FromUserID = peerId,
                 ToUserID = userId,
                 CreateDate = dt,
                 AssocCount = 0,
                 LastAssoc = dt,
                 InteractCount = 0,
                 Votes = 0
             };
         }
         else
             ptou.InteractCount++;
         lass.Add(ptou);
     }
     await uasvc.AddOrUpdateEntitiesAsync(cntx, new UserAssociationSet(), lass.ToArray());
 }
 /// <summary>
 /// Update changes to the current entity compared to an input <paramref name="newdata" /> and set the entity to a proper state for updating.
 /// </summary>
 /// <param name="newdata">The "new" entity acting as the source of the changes, if any.</param>
 /// <returns>
 /// </returns>
 public void UpdateChanges(UserAssociation newdata)
 {
     int cnt = 0;
     if (AssocCount != newdata.AssocCount)
     {
         AssocCount = newdata.AssocCount;
         IsAssocCountModified = true;
         cnt++;
     }
     if (AssocMemo != newdata.AssocMemo)
     {
         AssocMemo = newdata.AssocMemo;
         IsAssocMemoModified = true;
         cnt++;
     }
     if (DoNotNotify != newdata.DoNotNotify)
     {
         DoNotNotify = newdata.DoNotNotify;
         IsDoNotNotifyModified = true;
         cnt++;
     }
     if (InteractCount != newdata.InteractCount)
     {
         InteractCount = newdata.InteractCount;
         IsInteractCountModified = true;
         cnt++;
     }
     if (LastAssoc != newdata.LastAssoc)
     {
         LastAssoc = newdata.LastAssoc;
         IsLastAssocModified = true;
         cnt++;
     }
     if (LastInteract != newdata.LastInteract)
     {
         LastInteract = newdata.LastInteract;
         IsLastInteractModified = true;
         cnt++;
     }
     if (NoMessages != newdata.NoMessages)
     {
         NoMessages = newdata.NoMessages;
         IsNoMessagesModified = true;
         cnt++;
     }
     if (NotifyButBlock != newdata.NotifyButBlock)
     {
         NotifyButBlock = newdata.NotifyButBlock;
         IsNotifyButBlockModified = true;
         cnt++;
     }
     if (Votes != newdata.Votes)
     {
         Votes = newdata.Votes;
         IsVotesModified = true;
         cnt++;
     }
     IsEntityChanged = cnt > 0;
 }
 public static async Task<ConnectionStatus> UserConnected(string noticeHubId, string hubId, string peerId, string userId, string connectId, string languages)
 {
     var mbsvc = new UserAppMemberServiceProxy();
     var cntx = Cntx;
     cntx.AcceptLanguages = languages;
     var memb = await mbsvc.LoadEntityGraphRecursAsync(cntx, AppId, userId, null, null);
     if (memb != null)
     {
         memb.StartAutoUpdating = true;
         memb.LastActivityDate = DateTime.UtcNow;
         memb.AcceptLanguages = languages;
         List<MemberCallback> callbacks;
         if (memb.ChangedMemberCallbacks == null)
             callbacks = new List<MemberCallback>();
         else
             callbacks = new List<MemberCallback>(memb.ChangedMemberCallbacks);
         var cbk = (from d in callbacks where d.HubID == hubId && d.ChannelID == peerId select d).SingleOrDefault();
         if (cbk == null)
         {
             cbk = new MemberCallback
             {
                 ApplicationID = AppId,
                 UserID = userId,
                 HubID = hubId,
                 ChannelID = peerId,
                 ConnectionID = connectId,
                 IsDisconnected = false,
                 LastActiveDate = DateTime.UtcNow
             };
         }
         else
         {
             // it is very important to turn this on, otherwise the property will not be marked as modified.
             // and the service will not save the change!
             cbk.StartAutoUpdating = true;
             cbk.ConnectionID = connectId;
             cbk.IsDisconnected = false;
             cbk.LastActiveDate = DateTime.UtcNow;
         }
         memb.ChangedMemberCallbacks = new MemberCallback[] { cbk };
         await mbsvc.AddOrUpdateEntitiesAsync(cntx, new UserAppMemberSet(), new UserAppMember[] { memb });
         UserServiceProxy usvc = new UserServiceProxy();
         var u = await usvc.LoadEntityByKeyAsync(cntx, userId);
         memb.UserRef = u;
         var peerMb = await mbsvc.LoadEntityByKeyAsync(cntx, AppId, peerId);
         ConnectionStatus status = new ConnectionStatus();
         status.me = cbk;
         MemberCallbackServiceProxy mbcsvc = new MemberCallbackServiceProxy();
         UserAssociationServiceProxy uasvc = new UserAssociationServiceProxy();
         var peerCb = await mbcsvc.LoadEntityByKeyAsync(cntx, userId, hubId, AppId, peerId);
         if (peerCb == null || peerCb.ConnectionID == null || peerCb.IsDisconnected)
         {
             UserAssociation utop = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);
             MemberNotificationTypeServiceProxy ntsvc = new MemberNotificationTypeServiceProxy();
             var ntype = await ntsvc.LoadEntityByKeyAsync(cntx, ApplicationContext.PrivateChatNoticeTypeId);
             var notifier = await mbcsvc.LoadEntityByKeyAsync(cntx, "System", noticeHubId, AppId, peerId);
             if (notifier != null && notifier.ConnectionID != null && !notifier.IsDisconnected)
             {
                 if (utop != null && utop.NoMessages != null && utop.NoMessages == true)
                 {
                     status.status = PeerStatus.InBlackList;
                 }
                 else if (utop != null && utop.DoNotNotify != null && utop.DoNotNotify == true)
                 {
                     status.status = PeerStatus.DoNotDisturb;
                 }
                 else if (utop != null && utop.NotifyButBlock != null && utop.NotifyButBlock == true)
                 {
                     status.peerNotifier = notifier;
                     status.status = PeerStatus.NotifyButBlock;
                 }
                 else
                 {
                     status.peerNotifier = notifier;
                     status.status = PeerStatus.Notifiable;
                 }
             }
             else
             {
                 if (utop == null || utop.NoMessages == null || utop.NoMessages == false)
                     status.status = PeerStatus.LeaveMessage;
             }
             MemberNotification n = new MemberNotification 
             {
                 ID = Guid.NewGuid().ToString(),
                 Title = string.Format(ResourceUtils.GetString("20dc5913998d0e9ed01360475e46a0f9", "{0} invites you to chat, is waiting ...", peerMb.AcceptLanguages), ""),
                 CreatedDate = DateTime.UtcNow,
                 PriorityLevel = 0,
                 ReadCount = 0,
                 ApplicationID = AppId,
                 TypeID = ApplicationContext.PrivateChatNoticeTypeId,
                 UserID = peerId
             };
             bool hasIcon = !string.IsNullOrEmpty(memb.IconMime);
             n.NoticeMsg = "{ \"peerId\": \"" + userId + "\", \"peer\": \"" + u.Username + "\", \"connectId\": \"" + connectId + "\", \"hasIcon\": " + (hasIcon ? "true" : "false") + ", \"msg\": \"" + n.Title + "\", \"isCancel\": false, ";
             if (utop != null && utop.NoMessages != null && utop.NoMessages == true)
                 n.NoticeMsg += "\"noMessages\": true, ";
             else
                 n.NoticeMsg += "\"noMessages\": false, ";
             if (utop != null && utop.DoNotNotify != null && utop.DoNotNotify == true)
                 n.NoticeMsg += "\"notDisturb\": true, ";
             else
                 n.NoticeMsg += "\"notDisturb\": false, ";
             if (utop != null && utop.NotifyButBlock != null && utop.NotifyButBlock == true)
                 n.NoticeMsg += "\"keepNotified\": true }";
             else
                 n.NoticeMsg += "\"keepNotified\": false }";
             n.IsNoticeDataLoaded = true;
             MemberNotificationServiceProxy nsvc = new MemberNotificationServiceProxy();
             var r = await nsvc.AddOrUpdateEntitiesAsync(cntx, new MemberNotificationSet(), new MemberNotification[] { n });
             status.noticeType = ntype.TypeName;
             status.noticeMsg = n.NoticeMsg;
             status.noticeRecId = r.ChangedEntities[0].UpdatedItem.ID;
         }
         else
         {
             DateTime dt = DateTime.UtcNow;
             UserAssociation utop = await uasvc.LoadEntityByKeyAsync(cntx, userId, peerId, ApplicationContext.ChatAssocTypeId);
             if (utop == null)
             {
                 utop = new UserAssociation
                 {
                     TypeID = ApplicationContext.ChatAssocTypeId,
                     FromUserID = userId,
                     ToUserID = peerId,
                     CreateDate = dt,
                     AssocCount = 1,
                     LastAssoc = dt,
                     InteractCount = 0,
                     Votes = 0
                 };
             }
             else
                 utop.AssocCount++;
             UserAssociation ptou = await uasvc.LoadEntityByKeyAsync(cntx, peerId, userId, ApplicationContext.ChatAssocTypeId);
             if (ptou == null)
             {
                 ptou = new UserAssociation
                 {
                     TypeID = ApplicationContext.ChatAssocTypeId,
                     FromUserID = peerId,
                     ToUserID = userId,
                     CreateDate = dt,
                     AssocCount = 1,
                     LastAssoc = dt,
                     InteractCount = 0,
                     Votes = 0
                 };
             }
             else
                 ptou.AssocCount++;
             await uasvc.AddOrUpdateEntitiesAsync(cntx, new UserAssociationSet(), new UserAssociation[] { utop, ptou });
             status.status = PeerStatus.Connected;
         }
         if (peerCb != null)
             peerCb.UserAppMemberRef = peerMb;
         status.peer = peerCb;
         return status;
     }
     return null;
 }
 /// <summary>
 /// Merge changes inside entity <paramref name="from" /> to the entity <paramref name="to" />. Any changes in <paramref name="from" /> that is not changed in <paramref name="to" /> is updated inside <paramref name="to" />.
 /// </summary>
 /// <param name="from">The "old" entity acting as merging source.</param>
 /// <param name="to">The "new" entity which inherits changes made in <paramref name="from" />.</param>
 /// <returns>
 /// </returns>
 public static void MergeChanges(UserAssociation from, UserAssociation to)
 {
     if (to.IsPersisted)
     {
         if (from.IsAssocMemoModified && !to.IsAssocMemoModified)
         {
             to.AssocMemo = from.AssocMemo;
             to.IsAssocMemoModified = true;
         }
     }
     else
     {
         to.IsPersisted = from.IsPersisted;
         to.FromUserID = from.FromUserID;
         to.ToUserID = from.ToUserID;
         to.TypeID = from.TypeID;
         to.CreateDate = from.CreateDate;
         to.AssocMemo = from.AssocMemo;
         to.IsAssocMemoModified = from.IsAssocMemoModified;
     }
 }
 /// <summary>
 /// Update changes to the current entity compared to an input <paramref name="newdata" /> and set the entity to a proper state for updating.
 /// </summary>
 /// <param name="newdata">The "new" entity acting as the source of the changes, if any.</param>
 /// <returns>
 /// </returns>
 public void UpdateChanges(UserAssociation newdata)
 {
     int cnt = 0;
     if (AssocMemo != newdata.AssocMemo)
     {
         AssocMemo = newdata.AssocMemo;
         IsAssocMemoModified = true;
         cnt++;
     }
     IsEntityChanged = cnt > 0;
 }
        /// <summary>
        /// Internal use
        /// </summary>
        public UserAssociation ShallowCopy(bool allData = false, bool preserveState = false)
        {
            UserAssociation e = new UserAssociation();

            e.StartAutoUpdating = false;
            e.FromUserID        = FromUserID;
            e.ToUserID          = ToUserID;
            e.TypeID            = TypeID;
            e.CreateDate        = CreateDate;
            e.AssocCount        = AssocCount;
            if (preserveState)
            {
                e.IsAssocCountModified = IsAssocCountModified;
            }
            else
            {
                e.IsAssocCountModified = false;
            }
            e.InteractCount = InteractCount;
            if (preserveState)
            {
                e.IsInteractCountModified = IsInteractCountModified;
            }
            else
            {
                e.IsInteractCountModified = false;
            }
            e.LastAssoc = LastAssoc;
            if (preserveState)
            {
                e.IsLastAssocModified = IsLastAssocModified;
            }
            else
            {
                e.IsLastAssocModified = false;
            }
            e.LastInteract = LastInteract;
            if (preserveState)
            {
                e.IsLastInteractModified = IsLastInteractModified;
            }
            else
            {
                e.IsLastInteractModified = false;
            }
            e.Votes = Votes;
            if (preserveState)
            {
                e.IsVotesModified = IsVotesModified;
            }
            else
            {
                e.IsVotesModified = false;
            }
            if (allData)
            {
                e.AssocMemo = AssocMemo;
                if (preserveState)
                {
                    e.IsAssocMemoModified = IsAssocMemoModified;
                }
                else
                {
                    e.IsAssocMemoModified = false;
                }
            }
            e.DistinctString = GetDistinctString(true);
            e.IsPersisted    = IsPersisted;
            if (preserveState)
            {
                e.IsEntityChanged = IsEntityChanged;
            }
            else
            {
                e.IsEntityChanged = false;
            }
            e.StartAutoUpdating = true;
            return(e);
        }
 /// <summary>
 /// Internal use
 /// </summary>
 public UserAssociation ShallowCopy(bool allData = false)
 {
     UserAssociation e = new UserAssociation();
     e.IsInitializing = true;
     e.FromUserID = FromUserID;
     e.ToUserID = ToUserID;
     e.TypeID = TypeID;
     e.CreateDate = CreateDate;
     e.AssocMemo = AssocMemo;
     e.DistinctString = GetDistinctString(true);
     e.IsPersisted = true;
     e.IsEntityChanged = false;
     e.IsInitializing = false;
     return e;
 }