title: How to extend DbContext with partial class and partial OnModelCreating method in EntityFramework Core
body: <p>I'm using EF Core and DatabaseFirst approach. My dbContext is created automatically by <code>Scaffold-DbContext</code> command.</p>
  <p>I need to add some new DbSets into a dbContext and add into <code>OnModelCreating</code> method some additional code but after each scaffolding that added code are erased and I have to add it each time again.</p>
  <p>What I want to do is to create another partial dbContext class and mark <code>protected override void OnModelCreating(ModelBuilder modelBuilder)</code> method as partial</p>
  <p>but get errors:</p>
  <blockquote>
  <p>A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers.</p>
  </blockquote>
  <blockquote>
  <p>A partial method may not have multiple implementing declarations</p>
  </blockquote>
  <p>Here is a pseudo code:</p>
  <p><code>MyDbContext1.cs</code>  -  generated by <code>Scaffold-DbContext</code></p>
  <pre><code>public partial class MyDbContext : DbContext
  {
      public MyDbContext()
      {
      }
  
      public MyDbContext(DbContextOptions&lt;MyDbContext&gt; options)
          : base(options)
      {
      }
  
      public virtual DbSet&lt;Client&gt; Clients { get; set; }
  
      protected override partial void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Client&gt;(entity =&gt;
          {
              // some code ...
          }
      }
  }
  </code></pre>
  <p><code>MyDbContext2.cs</code> - this code I added each time into dbContext after scaffolding:</p>
  <pre><code>public partial class MyDbContext
  {
      public virtual DbSet&lt;JustAnotherEntity&gt; AnotherEntity { get; set; }
  
      protected override partial void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;JustAnotherEntity&gt;(entity =&gt;
          {
              entity.HasKey(e =&gt; new {e.Id, e.IdAction, e.IdState})
                  .ForSqlServerIsClustered(false);
          });
      }
  }
  </code></pre>