private void detach_LogIPs(LogIP entity)
		{
			this.SendPropertyChanging();
			entity.Identity = null;
		}
		private void attach_LogIPs(LogIP entity)
		{
			this.SendPropertyChanging();
			entity.Identity = this;
		}
 partial void DeleteLogIP(LogIP instance);
 partial void UpdateLogIP(LogIP instance);
 partial void InsertLogIP(LogIP instance);
        /// <summary>
        /// Creates a new session for these credentials; with a status of
        /// pending verification.
        /// </summary>
        public static LoginStatus CreateSession(string ipAddress, string username, 
            string password, int? lobbyId, bool debugMode, ref string callsignWithTags, out byte[] blackBoxData)
        {
            using (var db = new CSSDataContext())
            {
                blackBoxData = null;

                Login login;
                LoginStatus loginStatus;
                if (Login.TryGetAuthenticatedLogin(db, username, password, out login, out loginStatus) == false)
                    return loginStatus;

                if (lobbyId != null)
                {
                    //Ensure that the user has permission to log into this lobby
                    var lobby = db.Lobbies.FirstOrDefault(p => p.Id == lobbyId.GetValueOrDefault(0));
                    if (lobby == null || !lobby.IsEnabled)
                        return LoginStatus.PermissionDenied;
                    else if (lobby.IsRestrictive && !login.Lobby_Logins.Any(p => p.LobbyId == lobbyId.GetValueOrDefault(0)))
                        return LoginStatus.PermissionDenied;
                }

                // Ensure that the alias is in the user's list of available callsigns.
                var validAliases = login.Identity.Logins.SelectMany(p => p.Aliases).OrderBy(p => p.DateCreated).Take(Alias.GetAliasLimit(db, login));
                if (validAliases.Select(p => p.Callsign).Contains(Alias.GetCallsignFromStringWithTokensAndTags(db, callsignWithTags)) == false)
                {
                    if (validAliases.Count() > 0)
                        callsignWithTags = validAliases.FirstOrDefault().Callsign;
                    else
                        return LoginStatus.InvalidCredentials;
                }

                //Ensure that the alias is available for login
                Alias alias;
                if(Alias.ValidateUsage(db, login, true, password, ref callsignWithTags, out alias) != CheckAliasResult.Registered)
                    return LoginStatus.InvalidCredentials;

                //Destroy any existing sessions for this account
                db.Sessions.DeleteAllOnSubmit(login.Sessions);

                //Retrieve a blackbox for this user, if none exists, create one.
                var firstAvailableKey = RetrieveUnusedKey(db, login);

                if (firstAvailableKey == null)
                    firstAvailableKey = Task.GenerateBlackbox(db, debugMode);

                var root        = ConfigurationManager.AppSettings["OutputRoot"];
                var path        = Path.Combine(root, firstAvailableKey.Filename);
                blackBoxData    = File.ReadAllBytes(path);

                //Create a record of the usage of this key.
                var usedKey = new UsedKey()
                {
                    Login       = login,
                    ActiveKey   = firstAvailableKey,
                    DateUsed    = DateTime.Now
                };
                db.UsedKeys.InsertOnSubmit(usedKey);

                //string ipAddress = string.Empty;
                //var context = OperationContext.Current;
                //if (context != null)
                //{
                //    var remoteEndPoint = context
                //        .IncomingMessageProperties.[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                //    ipAddress = remoteEndPoint.Address;
                //}

                //Create a new session for this login
                login.Identity.DateLastLogin = DateTime.Now;
                var session = new Session()
                {
                    Login               = login,
                    Id                  = Guid.NewGuid(),
                    DateLastCheckIn     = DateTime.Now,
                    SessionStatusType   = SessionStatusEnum.PendingVerification,
                    ActiveKeyId         = firstAvailableKey.Id,
                    IPAddress           = ipAddress,
                    AliasId				= alias.Id
                };
                db.Sessions.InsertOnSubmit(session);

                // Add the identity's IP address to the log.
                var existingLogIP = db.LogIPs.FirstOrDefault(p => p.IdentityId == login.IdentityId && p.IPAddress == ipAddress);

                if (existingLogIP != null)
                {
                    existingLogIP.LastAccessed = DateTime.Now;
                }
                else
                {
                    existingLogIP = new LogIP()
                    {
                        LastAccessed = DateTime.Now,
                        IdentityId = login.IdentityId,
                        IPAddress = ipAddress
                    };

                    db.LogIPs.InsertOnSubmit(existingLogIP);
                }

                db.SubmitChanges();

                return LoginStatus.Authenticated;
            }
        }