public void Delete(MemberPoints item)
 {
     if (item != null)
     {
         ContextPerRequest.Db.MemberPoints.Remove(item);   
     }
 }
 public MemberPoints Add(MemberPoints points)
 {
     if (points.MemberId <= 0)
     {
         AppHelpers.LogError("Error adding point memberId null");
     }
     else
     {
         points.DateAdded = DateTime.UtcNow;
         ContextPerRequest.Db.MemberPoints.Add(points);
     }
     return points;
 }
        protected void UploadMembers(object sender, EventArgs e)
        {
            
            if (fuXml.HasFile)
            {
                // Upload file to App_Data temp folder
                var xmlFilepath = Server.MapPath("~/App_Data/TEMP/memberimport.xml");
                fuXml.SaveAs(xmlFilepath);

                // Once saved load xml
                var xml = new XmlDocument();
                xml.Load(xmlFilepath);

                // Now loop through the members
                var allMembers = xml.SelectNodes("//member");

                // Do we have any members
                if (allMembers != null && allMembers.Count > 0)
                {
                    // Store all mapped members
                    var members = new List<MemberImport>();

                    // Yes, we have members - So map them
                    foreach (XmlNode mem in allMembers)
                    {
                        var import = MapImport(mem);
                        if (import != null)
                        {
                            members.Add(import);   
                        }
                    }

                    //TODO - Refactor - Must be a more performant way of mass creating members

                    var memService = AppHelpers.UmbServices().MemberService;
                    var memHelper = AppHelpers.UmbMemberHelper();

                    var memberGroupService = AppHelpers.UmbServices().MemberGroupService;
                    var startingGroup = memberGroupService.GetByName(AppConstants.MemberGroupDefault);
                    var adminGroup = memberGroupService.GetByName(AppConstants.AdminRoleName);

                    var unitOfWorkManager = new UnitOfWorkManager(ContextPerRequest.Db);
                    var pointService = ServiceFactory.MemberPointsService;
                    using (var unitOfWork = unitOfWorkManager.NewUnitOfWork())
                    {
                        try
                        {
                            foreach (var newmem in members)
                            {
                                //var m = memService.CreateMemberWithIdentity("username", "email", "name", AppConstants.MemberTypeAlias);

                                var userToSave = memHelper.CreateRegistrationModel(AppConstants.MemberTypeAlias);
                                userToSave.Username = ServiceFactory.BannedWordService.SanitiseBannedWords(newmem.Username);
                                userToSave.Name = userToSave.Username;
                                userToSave.UsernameIsEmail = false;
                                userToSave.Email = newmem.Email;
                                userToSave.Password = Membership.GeneratePassword(10, 1);

                                MembershipCreateStatus createStatus;
                                memHelper.RegisterMember(userToSave, out createStatus, false);

                                if (createStatus == MembershipCreateStatus.Success)
                                {
                                    // Get the umbraco member
                                    var umbracoMember = memService.GetByUsername(userToSave.Username);

                                    // Set the role/group they should be in
                                    if (newmem.IsAdmin)
                                    {
                                        // Add to admin role
                                        memService.AssignRole(umbracoMember.Id, adminGroup.Name);

                                        // Add can edit member property
                                        umbracoMember.Properties[AppConstants.PropMemberCanEditOtherUsers].Value = newmem.IsAdmin;
                                    }
                                    else
                                    {
                                        // Standard role
                                        memService.AssignRole(umbracoMember.Id, startingGroup.Name);
                                    }

                                    // Add points
                                    if (newmem.Points > 0)
                                    {
                                        // Create the DB points
                                        var points = new MemberPoints
                                        {
                                            DateAdded = DateTime.Now,
                                            MemberId = umbracoMember.Id,
                                            Points = newmem.Points
                                        };
                                        pointService.Add(points);
                                    }

                                    // Do a save on the member
                                    memService.Save(umbracoMember);
                                }
                                else
                                {
                                    //TODO- Show errors to the user
                                }

                                Server.ScriptTimeout = 600;
                            }

                            // Commit the transaction
                            unitOfWork.Commit();

                            // Yes
                            pnlResults.Controls.Add(new LiteralControl("Success - Members Imported"));
                            pnlResults.Visible = true;   
                        }
                        catch (Exception ex)
                        {
                            // Roll back database changes 
                            unitOfWork.Rollback();

                            // No
                            pnlResults.Controls.Add(new LiteralControl("There was an error during import"));
                            pnlResults.Visible = true;   
                        }
                    }
                }
                else
                {
                    // No
                    pnlResults.Controls.Add(new LiteralControl("No members found in the XML"));
                    pnlResults.Visible = true;                    
                }
            }
            else
            {
                pnlResults.Controls.Add(new LiteralControl("No file selected"));
                pnlResults.Visible = true;
            }
        }