title: NUnit Entity Framework integration test dependency injection issue with .NET Core
body: <p>I have just started looking into .NET Core with Entity Framework. I have previously used .NET Framework with Ninject but I'm now trying to use the DI built into .NET Core.</p>
  
  <p>I have a <code>TestBase</code> class which my tests will derive from. I want this class to be responsible for creating and deleting a test database using <code>[OneTimeSetUp]</code> and <code>[OneTimeTearDown]</code>. The problem is that I don't seem to be able to figure out how to gain access to my DI services in the setup and teardown methods. These methods cannot have parameters and my <code>TestBase</code> class must have a parameterless constructor so I can't get them from there either.</p>
  
  <pre><code>[SetUpFixture]
  public partial class TestBase
  {
      protected IEFDatabaseContext DataContext { get; set; }
  
      public TestBase(IEFDatabaseContext dataContext)
      {
          this.DataContext = dataContext;
      }
  
      [OneTimeSetUp]
      public void TestInitialise()
      {
          this.DataContext.Database.EnsureCreated();
      }
  
      [OneTimeTearDown]
      public void TestTearDown()
      {
          this.DataContext.Database.EnsureDeleted();
      }
  }
  </code></pre>
  
  <p>The above gives the following error: </p>
  
  <blockquote>
    <p><code>TestBase</code> does not have a default constructor. </p>
  </blockquote>
  
  <p>I may well be going about this the wrong way but this is how I've always done things in the past so please let me know if there is a better method when using .NET Core DI.</p>
  
  <hr>
  
  <p><code>Startup</code> class for reference:</p>
  
  <pre><code>public class Startup
  {
      private readonly IConfiguration config;
  
      public Startup(IConfiguration config)
      {
          this.config = config;
      }
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext&lt;TestDataContext&gt;(
              options =&gt; options.UseSqlServer(this.config.GetConnectionString("TestConnectionString")),
              ServiceLifetime.Singleton);
  
          services.AddScoped&lt;IEFDatabaseContext&gt;(provider =&gt; provider.GetService&lt;TestDataContext&gt;());
      }
  }
  </code></pre>