title: Partial EF Context OnModelCreating Using Scaffold-DbContext
body: <p>I'm using doing database first development and using Scaffold-DbContext to create my entity models project. In that project, I have a partial dbContext where I override methods like SaveChangesAsync to set certain properties like, "LastModifiedBy".</p>
  
  <p>I'm looking to soft-delete records using dateDeleted/userDeleted columns. When I go to override the OnModelCreating routine, I see that it's already defined on the auto-generated partial context.</p>
  
  <p>I'm trying to do something like the following:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;SomeEntity&gt;().HasQueryFilter(x =&gt; x.DateDeleted == null);
  }
  </code></pre>
  
  <p>Does anyone have any suggestion as to how I can do this in a partial class way that still allows me to regenerate the original context on the fly using Scaffold-DbContext?</p>
  
  <p>Also note, I'm using .NET Core 2.1.5</p>
  
  <p><strong>ANSWER FROM David Browne - Microsoft</strong></p>
  
  <p>Add a static property on the partial context</p>
  
  <pre><code>public static bool GlobalFiltersAdded { get; set; } = false;
  </code></pre>
  
  <p>Then add a routine to add your filters:</p>
  
  <pre><code>private void AddGlobalFilters(ModelBuilder modelBuilder){
      SomeContext.GlobalFiltersAdded = true;
  }
  </code></pre>
  
  <p>Then in your generated context add the following:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      AddGlobalFilters(modelBuilder);
      //OTHER LOGIC WILL BE HERE
  }
  </code></pre>