title: Model first DbContext creation in Entity Framework 6
body: <p>In my web project I am trying to use Entity Framework 6 model first approach. Model is generated from existing database in separate project. For model generation connection string is saved in app.config of data access project. In main web project (ASP.NET Core MVC) 
  I am trying to inject context creation in Startup.cs class. </p>
  
  <p>Below is a code for context partial class. Partial class is used because context is auto generated from template.</p>
  
  <pre><code>[DbConfigurationType(typeof(EntityFrameworkConfig))]
  public partial class MyEntities
  {
      public MyEntities(String name) : base(name)
      {    
      }
  }
  </code></pre>
  
  <p>For using entity framework configuration from code instead app.config that cannot be used with asp.net core projects I have config class inherited from System.Data.Entity.DbConfiguration</p>
  
  <pre><code>public class EntityFrameworkConfig : DbConfiguration
  {
      public EntityFrameworkConfig()
      {
           this.SetDefaultConnectionFactory(new SqlConnectionFactory());
           this.SetProviderServices(SqlProviderServices.ProviderInvariantName,       SqlProviderServices.Instance);
      }
  }
  </code></pre>
  
  <p>In config.json in web project i have connection string:</p>
  
  <pre><code>{
      "MailSettings": {
      "ToAddress": "foo@bar.com"
      },
      "Connection": {
      "ConnectionString": "data source=SERVER;initial catalog=DB;user id=user;password=pwd;MultipleActiveResultSets=True;App=EntityFramework;"
      }
  }
  </code></pre>
  
  <p>In Startup.cs :</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      ...
  
      String connStr = _config["Connection:ConnectionString"];
      services.AddScoped((_) =&gt; new MyEntities(connStr));
      ...
  </code></pre>
  
  <p>}</p>
  
  <p>I am experiencing UnintentionalCodeFirstException thrown from OnModelCreating event of auto generated context class:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      throw new UnintentionalCodeFirstException();
  }
  </code></pre>
  
  <p>Is this proper way to use Entity framework 6 model first approach with asp.net Core MVC project and what is the reason for this exception ?</p>