How to extend DbContext with partial class and partial OnModelCreating method in EntityFramework Core : <p>I'm using EF Core DbFirst approach. My dbContext is created automatically by Scaffold-DbContext command. </p>

<p>I need to add additional DbSets into a dbContext and add into OnModelCreating method of dbContext 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 <strong><em>protected override void OnModelCreating(ModelBuilder modelBuilder)</em></strong> as a partial method </p>

<p>but get errors: </p>

<ol>
<li><strong>A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers</strong>.</li>
<li><strong>A partial method may not have multiple implementing declarations</strong></li>
</ol>

<p>Here is a pseudo code:</p>

<p>MyDbContext1.cs  -  generated by Scaffold-DbContext</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>MyDbContext2.cs - 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>
