title: You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class
body: <p>I can't make WebSecurity object work anywhere except what's already been generated in AccountController.cs file. Account controller has <code>[InitializeSimpleMembership]</code> attribute set at the top. Login functions don't complain about calling <code>WebSecurity.Login(...)</code>, for example. I added a child action to AccountController:</p>
  
  <pre><code>[ChildActionOnly]
          [AllowAnonymous]
          public ActionResult NavBar()
          {
              NavBarViewModel viewModel = new NavBarViewModel();
              viewModel.LinkItems = new List&lt;NavBarLinkItem&gt;();
  
              if (Request.IsAuthenticated)
              {
                  SimpleRoleProvider roleProvider = new SimpleRoleProvider();
                  if (roleProvider.IsUserInRole(User.Identity.Name, "User"))
                  {
                      viewModel.LinkItems.Add(new NavBarLinkItem() 
                      { Title = "Create Project", Action = "Create", Controller = "Project" });
  
                  }
  
              }
  
              viewModel.LinkItems.Add(new NavBarLinkItem() { Title="Help", Action="Index", Controller="Help" });
  
              return PartialView("_NavBar", viewModel);
          }
  </code></pre>
  
  <p>Left as is, the code crashes on "if (roleProvider.IsUserInRole(User.Identity.Name, "User"))" line with the subject error message. So I go into InitialzeSimpleMembershipAttribute.cs file and copy/paste this line at the top of my function:</p>
  
  <pre><code>WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
  </code></pre>
  
  <p>... and get an error message that WebSecurity.InitializeDatabaseConnection should only be called once. This makes sense, because there is an attribute at the top of the controller definition that should've called this function already (and it seems it does that just fine). So to be safe, I change above call to:</p>
  
  <pre><code>if (!WebSecurity.Initialized)
              {
                  WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId",
                                                           "UserName", autoCreateTables: true);
              }
  </code></pre>
  
  <p>... and get back the original error message, that WebSecurity.InitializeDatabaseConnection should be called before blah blah blah. Any insight into this madness would be greatly appreciated</p>