title: Adding a class which uses DbContext to ASP.NET Identity 2 project
body: <p>I'm working with ASP.NET MVC application which is based on Identity sample available via <a href="https://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples" rel="nofollow">NuGet</a>. Because of this I already have some classes to work with the database e.g. ApplicationDbContext. </p>
  
  <p>Say, I decided to let users leave requests for the administrator. I've added the Request class to the models:</p>
  
  <pre><code>public class Request
  {
      public int Id { get; set; }
      public string Message { get; set; }
      public ApplicationUser User { get; set; }
  }
  </code></pre>
  
  <p>Since the sample uses different managers to work with users, roles, etc, I've decided to create another one called ApplicationRequestManager inside the Identity.config file (though I'm not sure it's a good practice).</p>
  
  <pre><code> public class ApplicationRequestManager : IRequestManager
  {
  
      private ApplicationDbContext db = new ApplicationDbContext();
  
      public void Add(Request request)
      {
              db.Requests.Add(request);
              db.SaveChanges();            
      }
      ...
  }
  </code></pre>
  
  <p>This class uses the ApplicationDbContext to work with the database and has some methods to create a request, find it and so on.</p>
  
  <p>I've created a method responsible for sending request inside the Manage controller:</p>
  
  <pre><code>public ActionResult SendRequest(IndexViewModel model)
      {
          Request request = new Request { Message = model.Message, User = UserManager.FindById(User.Identity.GetUserId()) };
          requestManager.Add(request);
          return View();
      }
  </code></pre>
  
  <p>When this method is invoked, I get the following exception: </p>
  
  <blockquote>
    <p>An entity object cannot be referenced by multiple instances of IEntityChangeTracker</p>
  </blockquote>
  
  <p>If I understood correctly, the reason of exception is that I use one ApplicationDbContext to get User - via UserManager and I use another ApplicationDbContext to add the request - via RequestManager, so my request is attached to two contexts. As far as I know, such mistake can be avoided by passing the same context to both UserManager and RequestManager. However, UserManager gets its context via the OwinContext together with other managers:</p>
  
  <pre><code>// Configure the db context, user manager and role manager to use a single instance per request
          app.CreatePerOwinContext(ApplicationDbContext.Create);
          app.CreatePerOwinContext&lt;ApplicationUserManager&gt;(ApplicationUserManager.Create);
          app.CreatePerOwinContext&lt;ApplicationRoleManager&gt;(ApplicationRoleManager.Create);
          app.CreatePerOwinContext&lt;ApplicationSignInManager&gt;(ApplicationSignInManager.Create);
  </code></pre>
  
  <p>How can I make my own manager follow that pattern as well? I've tried to use the CreatePerOwinContext method like</p>
  
  <pre><code> app.CreatePerOwinContext&lt;ApplicationRequestManager&gt;(ApplicationRequestManager.Create);
  </code></pre>
  
  <p>And I've also tried to implement the Create method following the RoleManager example</p>
  
  <pre><code>public static ApplicationRoleManager Create(IdentityFactoryOptions&lt;ApplicationRoleManager&gt; options, IOwinContext context)
      {
          return new ApplicationRoleManager(new RoleStore&lt;ApplicationRole&gt;(context.Get&lt;ApplicationDbContext&gt;()));
      }
  </code></pre>
  
  <p>But I don't have any Store for my requests so I don't know what I should do with the 'new RoleStore' part. How could I solve that problem? </p>
  
  <p><strong>Updated:</strong></p>
  
  <p>I've tried Gert's solution and it worked: </p>
  
  <pre><code>public class Request
  {
    public int Id { get; set; }
    public string Message { get; set; }
  
    [ForeignKey("User")]
    public int ApplicationUserId { get; set; }
    public ApplicationUser User { get; set; }
  }
  
  var userId = User.Identity.GetUserId();
  Request request = new Request
                    { 
                        Message = model.Message,
                        ApplicationUserId = userId
                    };
  </code></pre>
  
  <p>I've also tired another way using HttpConext.Current.GetOwinContext().Get method. I've added the following line to my ApplicationRequestMananger:</p>
  
  <pre><code>public ApplicationRequestManager()
      {
          this.db = HttpContext.Current.GetOwinContext().Get&lt;ApplicationDbContext&gt;();
      }
  </code></pre>
  
  <p>And it worked fine with the original Request class. </p>
  
  <p>The question is, what advantages and disadvantages does each way have? I've read about foreign keys and I understand the general idea quite well; but I don't really understand what problems can 'HttpContext.Current.GetOwinContext().Get()' cause. Should I use it since it's simpler than adding foreign keys? </p>