public int Account_AddFromAspNetUser(string aspNetId, AppUser entity, ref string statusMessage)
        {
            EM.Account efEntity = new EM.Account();
            using (var context = new EntityContext())
            {
                try
                {
                    EM.AspNetUser user = AspNetUser_Get(entity.Email);

                    AppUser_FromMap(entity, efEntity);

                    efEntity.RowId = Guid.NewGuid();

                    efEntity.Created     = System.DateTime.Now;
                    efEntity.LastUpdated = System.DateTime.Now;

                    context.Accounts.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        statusMessage = "successful";
                        entity.Id     = efEntity.Id;

                        return(efEntity.Id);
                    }
                    else
                    {
                        //?no info on error
                        statusMessage = "Error - the add was not successful. ";
                        string message = string.Format("AccountManager. Account_Add Failed", "Attempted to add a Account. The process appeared to not work, but was not an exception, so we have no message, or no clue.Email: {0}", entity.Email);
                        EmailManager.NotifyAdmin("	Manager. Account_Add Failed", message);
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    //LoggingHelper.LogError( dbex, thisClassName + string.Format( ".ContentAdd() DbEntityValidationException, Type:{0}", entity.TypeId ) );
                    string message = thisClassName + string.Format(".Account_Add() DbEntityValidationException, Email: {0}", efEntity.Email);
                    foreach (var eve in dbex.EntityValidationErrors)
                    {
                        message += string.Format("\rEntity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                 eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            message += string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                     ve.PropertyName, ve.ErrorMessage);
                        }

                        LoggingHelper.LogError(message, true);
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Account_Add(), Email: {0}", efEntity.Email));
                }
            }

            return(efEntity.Id);
        }
        public bool Delete(int userId, ref string statusMessage)
        {
            using (var context = new EntityContext())
            {
                try
                {
                    EM.Account efEntity = context.Accounts
                                          .SingleOrDefault(s => s.Id == userId);
                    if (efEntity != null && efEntity.Id > 0)
                    {
                        efEntity.IsActive    = false;
                        efEntity.LastUpdated = System.DateTime.Now;
                        int count = context.SaveChanges();
                        //add activity log (usually in caller method)

                        //need to handle AspNetUsers
                        AspNetUsers_LockOut(efEntity.AspNetId, ref statusMessage);
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Delete(), userId: {0}", userId));
                }
            }

            return(false);
        }
        public bool Update(AppUser entity, ref string statusMessage)
        {
            using (var context = new EntityContext())
            {
                try
                {
                    EM.Account efEntity = context.Accounts
                                          .SingleOrDefault(s => s.Id == entity.Id);

                    if (efEntity != null && efEntity.Id > 0)
                    {
                        AppUser_FromMap(entity, efEntity);
                        context.Entry(efEntity).State = System.Data.Entity.EntityState.Modified;

                        if (HasStateChanged(context))
                        {
                            efEntity.LastUpdated = System.DateTime.Now;

                            // submit the change to database
                            int count = context.SaveChanges();
                            if (count > 0)
                            {
                                statusMessage = "successful";
                                //arbitrarily update AspNetUsers???
                                AspNetUsers_Update(entity, ref statusMessage);

                                return(true);
                            }
                            else
                            {
                                //?no info on error
                                statusMessage = "Error - the update was not successful. ";
                                string message = string.Format(thisClassName + ".Account_Update Failed", "Attempted to uddate a Account. The process appeared to not work, but was not an exception, so we have no message, or no clue.Email: {0}", entity.Email);
                                EmailManager.NotifyAdmin(thisClassName + ". Account_Update Failed", message);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Account_Update(), Email: {0}", entity.Email));
                }
            }

            return(false);
        }
        }         //

        //NOTE: AspNetRoles is to be a guid, so not likely to use this version
        //public void GetAllUsersInRole( int roleId )
        //{
        //	using ( var context = new EntityContext() )
        //	{
        //		var customers = context.AspNetUsers
        //			  .Where( u => u.AspNetUserRoles.Any( r => r.RoleId == roleId ) )
        //			  .ToList();
        //	}
        //}
        private static void AppUser_FromMap(AppUser fromEntity, EM.Account to)
        {
            to.Id = fromEntity.Id;
            //to.RowId = fromEntity.RowId;
            to.UserName = fromEntity.UserName;
            to.AspNetId = fromEntity.AspNetUserId;
            to.Password = fromEntity.Password;
            to.IsActive = fromEntity.IsActive;

            to.FirstName           = fromEntity.FirstName;
            to.LastName            = fromEntity.LastName;
            to.Email               = fromEntity.Email;
            to.CEAccountIdentifier = fromEntity.CEAccountIdentifier;

            to.Created         = fromEntity.Created;
            to.LastUpdated     = fromEntity.LastUpdated;
            to.LastUpdatedById = fromEntity.LastUpdatedById;
        }
        public static AppUser Get(int Id)
        {
            AppUser entity = new AppUser();

            using (var context = new EntityContext())
            {
                //this method may be bringing back to much info - evaluate
                EM.Account item = context.Accounts
                                  .SingleOrDefault(s => s.Id == Id);

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity);
                }
            }

            return(entity);
        }
        }         //

        public static void MapFromDB(EM.Account fromEntity, AppUser to)
        {
            to.Id = fromEntity.Id;
            //to.RowId = fromEntity.RowId;
            to.UserName = fromEntity.UserName;

            to.AspNetUserId = fromEntity.AspNetId;
            //to.Password = fromEntity.Password;
            to.IsActive  = fromEntity.IsActive == null ? false : ( bool )fromEntity.IsActive;
            to.FirstName = fromEntity.FirstName;
            to.LastName  = fromEntity.LastName;

            to.Email           = fromEntity.Email;
            to.Created         = fromEntity.Created;
            to.LastUpdated     = fromEntity.LastUpdated;
            to.LastUpdatedById = fromEntity.LastUpdatedById == null ? 0 : ( int )fromEntity.LastUpdatedById;

            to.UserRoles = new List <string>();
            if (fromEntity.AspNetUser != null)
            {
                foreach (EM.AspNetUserRole role in fromEntity.AspNetUser.AspNetUserRoles)
                {
                    to.UserRoles.Add(role.AspNetRole.Name);
                }
            }

            if (fromEntity.Organization_Member != null && fromEntity.Organization_Member.Count > 0)
            {
                foreach (EM.Organization_Member mbrs in fromEntity.Organization_Member)
                {
                    //just a light version for now
                    to.Organizations.Add(new MC.Organization()
                    {
                        Id   = mbrs.Organization.Id,
                        Name = mbrs.Organization.Name,
                        CTID = mbrs.Organization.CTID
                    });
                }
            }
        }         //