title: How to use StructureMap Registry to Configure EF Core in Separate Project?
body: <p>I'm trying to set up a solution in Visual Studio that has all of the EF Core files in one project (Infrastructure), separate from my ASP.NET Core web project. Further, I want to prevent developers from accidentally using types from the Infrastructure project, so I'm attempting to avoid having a reference from Web to Infrastructure. This is easily done in ASP.NET 5 / EF 6 as described here:
  <a href="https://ardalis.com/avoid-referencing-infrastructure-in-visual-studio-solutions" rel="noreferrer">https://ardalis.com/avoid-referencing-infrastructure-in-visual-studio-solutions</a></p>
  
  <p>In ASP.NET Core, however, we configure EF Core in the web project's Startup.ConfigureServices() method:</p>
  
  <pre><code>    public IServiceProvider ConfigureServices(IServiceCollection services)
      {
          string dbName = Guid.NewGuid().ToString();
          services.AddDbContext&lt;AppDbContext&gt;(options =&gt;
              options.UseInMemoryDatabase(dbName));
  
          services.AddMvc()
              .AddControllersAsServices();
  
          // use StructureMap
          var container = new Container();
          container.Configure(config =&gt;
          {
              config.Scan(_ =&gt;
              {
                  _.AssembliesAndExecutablesFromApplicationBaseDirectory();
                  _.WithDefaultConventions();
                  _.LookForRegistries();
              });
  
              config.Populate(services);
          });
  
          return container.GetInstance&lt;IServiceProvider&gt;();
      }
  </code></pre>
  
  <p>Is there a way to move the <code>AddDbContext</code> logic out of <code>Startup</code> and into my Infrastructure project? I have a StructureMap registry in the Infrastructure project already, so if there's a way to wire up the required services from there, it will have access to the DbContext type since it's in that project:</p>
  
  <pre><code>public class InfrastructureRegistry : Registry
  {
      public InfrastructureRegistry()
      {
          For(typeof(IRepository&lt;&gt;)).Add(typeof(EfRepository&lt;&gt;));
      }
  }
  </code></pre>
  
  <p>You can view the current state of the project in this branch:
  <a href="https://github.com/ardalis/cleanarchitecture/tree/ardalis/projectref" rel="noreferrer">https://github.com/ardalis/cleanarchitecture/tree/ardalis/projectref</a></p>
  
  <p>My goal is to remove the project reference from Web to Infrastructure but still have the solution load the AppDbContext when it starts up.</p>