title: C# EF 4.1 Create table dynamically within DbContext
body: <p>I want to add tables to a SQLCe database at runtime, since the tablenames are not static and known at compile time.
  I try to do this with Entity Framework 4.1 and DbContext as follows:</p>
  
  <pre><code>public class PersonContext : DbContext
  {
      public PersonContext()
          : base("UnicornsCEDatabase")
      {
      }
  }
  public class Person
  {
      public int NameId { get; set; }
      public string Name { get; set; }
  }
  public class Program
  {
      static void Main(string[] args)
      {
          using (var db = new PersonContext())
          {
              db.Database.Delete();
  
              //Try to create table
              DbSet per = db.Set&lt;Person&gt;();
              var per1 = new Person { NameId = 1, Name = "James"};
              per.Add(per1);
              int recordsAffected = db.SaveChanges();
  
              Console.WriteLine(
                  "Saved {0} entities to the database, press any key to exit.",
                  recordsAffected);
              Console.ReadKey();
          }
      }
  }
  </code></pre>
  
  <p>When trying to run this it throws this error:
  <em>The entity type Person is not part of the model for the current context</em>.</p>
  
  <p>Is it possible to add a DbSet at runtime to the DbContext <strong>without</strong> having to define that DbSet(with its schema) in the database?</p>
  
  <p>When defining the DbContext statically with Person, EF will create the entire database and the tables on the fly, which is great.</p>
  
  <p>For example:</p>
  
  <pre><code>foreach (var item in collection)
  {
     string tableName = "PersonTable_"+item.Name;
     //Add a table with the name tableName to DbContext
  }
  </code></pre>
  
  <p>Is this somehow possible with EF or do I have to create these with some other technique?</p>
  
  <p>Thanks, Juergen</p>