title: ASP.NET Core DI with Simple Injector sharing DbContext
body: <p>I'm setting up an ASP.NET Core project and following a CQRS pattern basing my work on the <a href="https://github.com/danludwig/tripod" rel="nofollow noreferrer">Tripod</a> project. I've followed the Simple Injector integration guide but a little puzzled on one point... I want to keep make use of the <code>UseInMemoryDatabase</code> option for testing and can only find examples of that using the Core DI <code>AddDbContext</code> method.</p>
  
  <p>My DbContext implement several interfaces:</p>
  
  <pre><code>public class EntityDbContext : DbContext,
      IUnitOfWork,
      IReadEntities,
      IWriteEntities
  {
      // code here
  }
  </code></pre>
  
  <p>I'm doing this in the <code>Startup.ConfigureServices</code> method:</p>
  
  <pre><code>services.AddDbContext&lt;EntityDbContext&gt;(options =&gt; options.UseInMemoryDatabase("Snoogans"));
  </code></pre>
  
  <p>and following the SI integration guide this in the Startup.Configure method:</p>
  
  <pre><code>container.Register&lt;IUnitOfWork, xxxx&gt;();
  container.Register&lt;IReadEntities, xxxx&gt;();
  container.Register&lt;IWriteEntities, xxxx&gt;();
  </code></pre>
  
  <p>Am I able to get the Core registration over crosswire to plugin to the target for each of them or should I just be registering the context directly in SI?</p>
  
  <p>==
  I'm playing around with the original concept from Tripod:</p>
  
  <pre><code>var contextRegistration =
      lifestyle.CreateRegistration&lt;EntityDbContext, EntityDbContext&gt;(container);
  container.AddRegistration(typeof(EntityDbContext), contextRegistration);
  container.AddRegistration(typeof(IUnitOfWork), contextRegistration);
  container.AddRegistration(typeof(IWriteEntities), contextRegistration);
  container.AddRegistration(typeof(IReadEntities), contextRegistration);
  </code></pre>
  
  <p>trying to do everything with SI, but not sure how I get at the Registration for the 3 interfaces:</p>
  
  <pre><code>container.Register&lt;EntityDbContext&gt;(() =&gt;
  {
      var optionsBuilder =
          new DbContextOptionsBuilder&lt;EntityDbContext&gt;().UseInMemoryDatabase("Snoogans");
      return new EntityDbContext(optionsBuilder.Options);
  });
  container.AddRegistration&lt;IUnitOfWork&gt;(xxxx);
  container.AddRegistration&lt;IReadEntities&gt;(xxxx);
  container.AddRegistration&lt;IWriteEntities&gt;(xxxx);
  </code></pre>