title: Create Tables on runtime in EF Core
body: <p>I know I can use the following command to add a new migration and create the database :</p>
  <pre><code>dotnet ef migrations add MigrationName -c DbContextName
  dotnet ef database update -c DbContextName
  </code></pre>
  <p>but I need to create my database/tables at runtime.</p>
  <p>First I tried to do that by overriding OnModelCreating but I failed. It returned me an error pointing out that the tables have not been created</p>
  <p>Here is my dbcontext class</p>
  <pre><code>public class AimeLoggerContext : DbContext
      {
          public AimeLoggerContext(DbContextOptions&lt;AimeLoggerContext&gt; options)
              : base(options)
          {
              Database.Migrate();
          }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              base.OnConfiguring(optionsBuilder);
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity(&quot;Aime.Logger.Loggers.Database.Model.Log&quot;, b =&gt;
              {
                  b.Property&lt;int&gt;(&quot;Id&quot;)
                    .ValueGeneratedOnAdd();
                  b.Property&lt;DateTime&gt;(&quot;Datetime&quot;);
                  b.HasKey(&quot;Id&quot;);
                  b.ToTable(&quot;Logs&quot;);
              });
  
              base.OnModelCreating(modelBuilder);
          }
  
          public DbSet&lt;Log&gt; Logs { get; set; }
      }
  </code></pre>