title: How do I implement DbContext inheritance for multiple databases in EF7 / .NET Core
body: <p>I am building web APIs in ASP.NET Core 1.1.</p>
  
  <p>I have a number different databases (for different systems) which have common base schemas for configuration items such as Configuration, Users and groups (about 25 tables in all).  I am trying to avoid duplicating the quite extensive EF configuration for the shared part of the model by inheriting from a base class as shown in the diagram.</p>
  
  <p><a href="https://i.stack.imgur.com/mE7SK.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/mE7SK.jpg" alt="My DbContext inheritance tree"></a></p>
  
  <p>However, this does not work because of the Entity Framework (EF) requirement to pass <code>DbContextOptions&lt;DerivedRepository&gt;</code> as a parameter to the constructor, where <code>DerivedRepository</code> must match the type of the repository the constructor is called on.  The parameter must then be passed down to the base <code>DbContext</code> by calling <code>:base(param)</code>.</p>
  
  <p>So when (for example) InvestContext is initialised with <code>DbContextOptions&lt;InvestContext&gt;</code>, it calls <code>base(DbContextOptions&lt;InvestContext&gt;)</code> and EF throws an error because the call to the <code>ConfigurationContext</code> constructor is receiving a parameter of type <code>DbContextOptions&lt;InvestContext&gt;</code> instead of the required type <code>DbContextOptions&lt;ConfigurationContext&gt;</code>.  Since the options field on DbContext is defined as </p>
  
  <pre><code>    private readonly DbContextOptions _options;
  </code></pre>
  
  <p>I can't see a way around this.</p>
  
  <p>What is the best way to define the shared model once and use it multiple times?  I guess I could create a helper function and call it from every derived context, but it's not nearly as clean or transparent as inheritance.</p>