title: Creating 2 sides relationship between a model to the default ApplicationUser of a basic MVC 5 project. Working with 2 dbContext classes
body: <p>I believe it's suppose to be a trivial issue, but I didn't find any relevant answer.</p>
  
  <p>I am working on a MVC 5 project  (created by the default VS template with Identity).</p>
  
  <p>I have a Post model. Only an Authorized "user" ("user" is the default ApplicationUser class provided by the EF template when creating the project) can create a post.</p>
  
  <p><strong>I need to Extend the post and the user models in that way:</strong></p>
  
  <p>-Every Post will have the user (who created it)</p>
  
  <p>-Every user will have all the Posts were created by him.</p>
  
  <p>In other words, I need a one to many relationship between Post and users.
  (my goal is that only post's owner will be able to edit it. Moreover, every "user" can get a list of his posts )</p>
  
  <p>I use EntityFramework 6.1.2 with Identity 2.0 (or 1.0.. not sure how to check?).</p>
  
  <p>The problem:</p>
  
  <p>I have 2 dbContext classes pointing to two different db tables:</p>
  
  <p>1) <strong>ApplicationDbContext</strong> - The default DbContext provided by the EF when the project was created and points to the AspNetUsers table.</p>
  
  <p>2) <strong>MrSaleDB</strong> - The DbContext which points to the Posts db-table (and other tables in my model like Galleries and etc. )</p>
  
  <p>So how should I extend the Post and user classes? 
   <strong><em>How can I compare the current user identity to the user who was saved in the extended Post class (in the PostController while user edit a post) ?</em></strong></p>
  
  <p>Everything I tried didn't work )-;</p>
  
  <p>Here are some code from my project, thanks: </p>
  
  <p>Models:</p>
  
  <pre><code>namespace MrSaleBeta01.Models
  {
  // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
  **public class ApplicationUser : IdentityUser**
  {
      public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
      {
          // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
          var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
          // Add custom user claims here
          return userIdentity;
      }
  
  
  //ADDED:
  
      public ApplicationUser ()
      {
          Posts = new List&lt;Post&gt;();
      }
  
      public ICollection Posts { get; set; }
  }
  
  
  **public class Post**
  {
      [Key]
      public int Id { get; set; }        
  
      [Required]
      public string Title { get; set; }
  
      public virtual Gallery Gallery { get; set; }
  
      [ForeignKey("Category")]
      public int CategoryId { get; set; }
      // Navigation property:
      public virtual Category Category { get; set; }
  
  
  //ADDED:
      public virtual ApplicationUser PublishedBy { get; set; }
  
  
  }   
  
  }
  </code></pre>
  
  <p>DbContexts:</p>
  
  <pre><code>namespace MrSaleBeta01.Models
  {
  
  /*
  * First (default) DbContext, points to users tables:
  */
  public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public ApplicationDbContext()
          : base("DefaultConnection", throwIfV1Schema: false)
      {
      }
  
  ...
  }
  
  
  /*
  * Second DbContext, points to my models tables:
  */  
  public class MrSaleDB : DbContext
  {
  
      public MrSaleDB() : base("name=MrSaleDB")
      {
      }
  
      public DbSet&lt;Post&gt; Posts { get; set; }
  
      public DbSet&lt;Category&gt; Categories { get; set; }
  
      public DbSet&lt;AreaCode&gt; AreaCodes { get; set; }
  
      public DbSet&lt;Gallery&gt; Galleries { get; set; }
   ...
  }       
  
  }
  </code></pre>
  
  <p>PostsController:</p>
  
  <pre><code>    // POST: Posts/Edit/5
      [HttpPost]
      [ValidateAntiForgeryToken]
      [Authorize]
      public ActionResult Edit(Post post)
      {
          try
          {
  
              if (ModelState.IsValid)
              {
  
  ...
  ...
  ...
  
                  //General: save post:
                  db.Posts.Add(post);
  
                  db.SaveChanges();
  
                  return RedirectToAction("Index");
              }
          }
          catch (DbEntityValidationException ex)
          {
          }
      }
  </code></pre>