title: Entity Framework - The context cannot be used while the model is being created
body: <p>I am trying to create a console application where I am trying to use Entity Framework to dynamically create database and tables to store the data. However, when I am trying to add data to my <code>DbSet</code>. I am getting the following error.</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.InvalidOperationException'
    occurred in EntityFramework.dll</p>
    
    <p>Additional information: The context cannot be used while the model is
    being created. This exception may be thrown if the context is used
    inside the OnModelCreating method or if the same context instance is
    accessed by multiple threads concurrently. Note that instance members
    of DbContext and related classes are not guaranteed to be thread safe.</p>
  </blockquote>
  
  <p>I have referred to other posts with similar error but none of the solutions worked. This is a single threaded application. </p>
  
  <p>The error occurs at line </p>
  
  <pre><code>this.context.Environments.Add(entity)
  </code></pre>
  
  <p><strong>DataSource.cs</strong></p>
  
  <pre><code>public class DataSource
  {
          private static DataSource instance;
  
          public DataSource()
          {
              this.context = new CounterContext();
          }
  
          public static DataSource Instance
          {
              get
              {
                  return instance ?? (instance = new DataSource());
              }
          }
  
          private CounterContext context;
  
          public void AddCockpitEnvironmentDetails(IList&lt;Environment&gt; environmentList)
          {
              foreach (var entity in environmentList)
              {
                  this.context.Environments.Add(entity);
              }
  
              this.context.SaveChanges();
          }
  ...
  }
  </code></pre>
  
  <p>My context:</p>
  
  <pre><code>public class CounterContext : DbContext
  {
          public CounterContext()
              : base("name=CounterDbString")
          {
              Database.SetInitializer&lt;CounterContext&gt;(new CreateDatabaseIfNotExists&lt;CounterContext&gt;());
          }
          public DbSet&lt;CounterData&gt; CounterDetails { get; set; }
  
          public DbSet&lt;Environment&gt; Environments { get; set; }
      }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public class Environment
  {
      [Key]
      public Guid Id { get; set; }
      public string EnvironmentName { get; set; }
      public string EnvironmentNotes { get; set; }
  
      public virtual ICollection&lt;CounterDetail&gt; CounterDetails { get; set; }
  };
  
  public class CounterData 
  {
      [Key]
      [Column(Order = 1)]
      public DateTime counterTime { get; set; }
      [Key]
      [Column(Order = 2)]
      public int counterName { get; set; }
      [Key]
      [Column(Order = 3)]
      public Guid EnvId {get; set;}
      public int count { get; set; }
  
      [ForeignKey("EnvId")]
      public virtual CockpitEnvironment Machine { get; set; }
  }
  </code></pre>
  
  <p><code>App.config</code>:</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="CounterDbString" 
           connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Counters_Test;Integrated Security=True;Connect Timeout=30"
           providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>