title: Add new table to an existing database using Code First approach in EF 6 and MVC 5
body: <p>I know this should be simple, but I was unable to find correct tutorial or explanation on web on this subject. There is a lot of videos and posts about adding new column to an existing table, using code first approach, but I can not find any with step by step explanation on how to add whole new table into existing database.
  Which is weird, I was pretty sure that I will find many examples. Maybe my search criteria is bad. So if anybody has any good link or video, please share.</p>
  
  <p>What I'm trying to do is to add table Post into existing Default Database, created by MVC 5 project.
  I've made model class first, named it Post and it has a simple definition:</p>
  
  <pre><code>public class Post
      {
          [Key]
          public int PostId { get; set; }
          public string PostText { get; set; }
          public byte[] ImagePost { get; set; }
          public byte[] FilePost { get; set; }
          public string TextPost { get; set; }
          public string UserId { get; set; }
      } 
  </code></pre>
  
  <p>Then I have first doubts. I know I should create DbContext, but if I want to hit an existing database, should I use existing DbContext, already created by default in Web.config file like this:</p>
  
  <pre><code>&lt;add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-StudentBookApp-20150114035149.mdf;Initial Catalog=aspnet-StudentBookApp-20150114035149;Integrated Security=True"
        providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>Or I should use all new DbContext created for Post class?</p>
  
  <p>Anyhow I've tried it both, but no success. When I created this new context I was doing following in PMC:</p>
  
  <pre><code>Enable-Migrations
  Add-Migration "PostMigration"
  Update-Database
  </code></pre>
  
  <p>Usually, or all goes well, but I don't get any new table in database, or I get an error: AspNetUsers already exists, and AspNetUsers is one of auto-created tables that I've changed by adding new columns to it.</p>
  
  <p>Update: My context right now is in seperated class named PostContext and it looks like this:</p>
  
  <pre><code>public class PostContext : DbContext
  {
      public PostContext() : base("name=PostContext")
      {
      }
  
      public DbSet&lt;Post&gt; Posts { get; set; }
  }
  </code></pre>
  
  <p><strong>Second try:</strong></p>
  
  <p>Since my first approach didn't gave any result. I've tried doing what's described in this <a href="https://stackoverflow.com/questions/23662755/how-to-add-new-table-to-existing-database-code-first">link</a>. Adding mapping to my project:</p>
  
  <p>I've crated new class PostConfiguration for mapping:</p>
  
  <pre><code>public class PostConfiguration : EntityTypeConfiguration&lt;Post&gt;
      {
          public PostConfiguration() : base()
          {
              HasKey(p =&gt; p.PostId);
              ToTable("Post");
  
              HasOptional(p =&gt; p.PostText);
              ToTable("Post");
  
              HasOptional(p =&gt; p.ImagePost);
              ToTable("Post");
  
              HasOptional(p =&gt; p.TextPost);
              ToTable("Post");
  
              HasOptional(p =&gt; p.FilePost);
              ToTable("Post");
  
              HasRequired(p =&gt; p.UserId);
              ToTable("Post");
          }
      }
  </code></pre>
  
  <p>In the class Post I've added context class too PostContext with modelBuilder variable as they've suggested in listed link above:</p>
  
  <pre><code> public class PostContext : DbContext
      {
          static PostContext()
          {
              Database.SetInitializer(new DropCreateDatabaseIfModelChanges&lt;PostContext&gt;());
          }
  
          public DbSet&lt;Post&gt; Posts { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Properties()
                          .Where(p =&gt; p.Name == "Key")
                          .Configure(p =&gt; p.IsKey());
  
              modelBuilder.Configurations.Add(new PostConfiguration());
          }
  
     }
  </code></pre>
  
  <p>I've run all PMC commands again, and still no success, table Post is missing from default database.</p>