title: An exception of type 'System.ArgumentNullException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll
body: <p>I am using ASP.Net Identity to create an app with roles based authentication. I want to create some custom roles. When I do that I get the following exception. But I'm unable to figure what's wrong here. Since I'm new to this Please do help me out. Thanks in advance. </p>
  
  <p>I get the exception in
  var appRoleManager = new ApplicationRoleManager(new RoleStore(context.Get())); 
  which is in the ApplicationRoleManager.cs class</p>
  
  <pre><code>using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  using Microsoft.AspNet.Identity.Owin;
  using Microsoft.Owin;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace EasyMaintain.SecurityWebAPI
  {
      public class ApplicationRoleManager : RoleManager&lt;IdentityRole&gt;
      {
  
          public ApplicationRoleManager(IRoleStore&lt;IdentityRole, string&gt; roleStore)
              : base(roleStore)
          {
          }    
  
          //create instances for each request
          public static ApplicationRoleManager Create(IdentityFactoryOptions&lt;ApplicationRoleManager&gt; options, IOwinContext context)
          {    
  
              var appRoleManager = new ApplicationRoleManager(new RoleStore&lt;IdentityRole&gt;(context.Get&lt;AuthContext&gt;()));
  
              return appRoleManager;
          }
      }
  }
  </code></pre>
  
  <p>The RoleModel.cs </p>
  
  <pre><code>using Microsoft.AspNet.Identity.EntityFramework;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net.Http;
  using System.Text;
  using System.Threading.Tasks;
  using System.Web.Http.Routing;
  
  namespace EasyMaintain.SecurityWebAPI.Models
  {
     public class RoleModel
      {
          private UrlHelper _UrlHelper;
          private ApplicationUserManager _AppUserManager;
  
  
          public RoleModel(HttpRequestMessage request, ApplicationUserManager appUserManager)
          {
              _UrlHelper = new UrlHelper(request);
              _AppUserManager = appUserManager;
          }
          public RoleReturnModel Create(IdentityRole appRole)
          {
              return new RoleReturnModel
              {
                  Url = _UrlHelper.Link("GetRoleById", new { id = appRole.Id }),
                  Id = appRole.Id,
                  Name = appRole.Name
              };
          }
      }
  
      public class RoleReturnModel
      {
          public string Url { get; set; }
          public string Id { get; set; }
          public string Name { get; set; }
      }
  
  }
  </code></pre>
  
  <p>The RoleController.cs</p>
  
  <pre><code>using EasyMaintain.SecurityWebAPI.Models;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Web.Http;
  using static EasyMaintain.SecurityWebAPI.Models.RoleBindingModels;
  
  namespace EasyMaintain.SecurityWebAPI.Controllers
  {
      [Authorize(Roles = "SuperAdmin")]
      [RoutePrefix("api/roles")]
      public class RolesController : BaseApiController
      {
          // GET api/roles
          [Route("{id:guid}", Name = "GetRoleById")]
  
          public async Task&lt;IHttpActionResult&gt; GetRole(string Id)
              {
                  var role = await this.AppRoleManager.FindByIdAsync(Id);
  
                  if (role != null)
                  {
                      return Ok(TheModelFactory.Create(role));
                  }
                  return NotFound();
              }
          //GET api/roles/5
          [Route("", Name = "GetAllRoles")]
  
              public IHttpActionResult GetAllRoles()
              {
                  var roles = this.AppRoleManager.Roles;
  
                  return Ok(roles);
              }
          // POST api/roles
          [Route("create")]
              [HttpPost]
              public async Task&lt;IHttpActionResult&gt; Create(RoleBindingModels model)
              {
                  if (!ModelState.IsValid)
                  {
                      return BadRequest(ModelState);
                  }
  
                  var role = new IdentityRole { Name = model.Name };
  
                  var result = await this.AppRoleManager.CreateAsync(role);
  
                  if (!result.Succeeded)
                  {
                      return GetErrorResult(result);
                  }
  
                  Uri locationHeader = new Uri(Url.Link("GetRoleById", new { id = role.Id }));
  
                  return Created(locationHeader, TheModelFactory.Create(role));
  
              }
  
              [Route("{id:guid}")]
              public async Task&lt;IHttpActionResult&gt; DeleteRole(string Id)
              {
                  var role = await this.AppRoleManager.FindByIdAsync(Id);
  
                  if (role != null)
                  {
                      IdentityResult result = await this.AppRoleManager.DeleteAsync(role);
                      if (!result.Succeeded)
                      {
                          return GetErrorResult(result);
                      }    
                      return Ok();
                  }
                  return NotFound();
              }
  
              [Route("ManageUsersInRole")]
              public async Task&lt;IHttpActionResult&gt; ManageUsersInRole(UsersInRoleModel model)
              {
                  var role = await this.AppRoleManager.FindByIdAsync(model.Id);
  
                  if (role == null)
                  {
                      ModelState.AddModelError("", "Role does not exist");
                      return BadRequest(ModelState);
                  }
  
                  foreach (string user in model.EnrolledUsers)
                  {
                      var appUser = await this.AppUserManager.FindByIdAsync(user);
  
                      if (appUser == null)
                      {
                          ModelState.AddModelError("", String.Format("User: {0} does not exists", user));
                          continue;
                      }
  
                      if (!this.AppUserManager.IsInRole(user, role.Name))
                      {
                          IdentityResult result = await this.AppUserManager.AddToRoleAsync(user, role.Name);
  
                          if (!result.Succeeded)
                          {
                              ModelState.AddModelError("", String.Format("User: {0} could not be added to role", user));
                          }
                      }
                  }
  
                  foreach (string user in model.RemovedUsers)
                  {
                      var appUser = await this.AppUserManager.FindByIdAsync(user);
  
                      if (appUser == null)
                      {
                          ModelState.AddModelError("", String.Format("User: {0} does not exists", user));
                          continue;
                      }
  
                      IdentityResult result = await this.AppUserManager.RemoveFromRoleAsync(user, role.Name);
  
                      if (!result.Succeeded)
                      {
                          ModelState.AddModelError("", String.Format("User: {0} could not be removed from role", user));
                      }
                  }
  
                  if (!ModelState.IsValid)
                  {
                      return BadRequest(ModelState);
                  }
                  return Ok();
              }
      }
  }
  </code></pre>