title: How do I configure a DbContext with dependency injection with .Net Core 2.1?
body: <p>Service</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      string cs = Configuration.GetConnectionString("Skillcheck"); 
      services.AddDbContext&lt;TicketsystemContext&gt;(options =&gt; options.UseSqlServer(cs));
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  }
  </code></pre>
  
  <p>Context</p>
  
  <pre><code>public TicketsystemContext()
  {
  }
  
  public TicketsystemContext(DbContextOptions&lt;TicketsystemContext&gt; options)
      : base(options)
  {
  }
  
  // ... rest of the context
  </code></pre>
  
  <p>Exception</p>
  
  <blockquote>
    <p>System.InvalidOperationException: No database provider has been
    configured for this DbContext. A provider can be configured by
    overriding the DbContext.OnConfiguring method or by using AddDbContext
    on the application service provider. If AddDbContext is used, then
    also ensure that your DbContext type accepts a
    DbContextOptions object in its constructor and passes it to
    the base constructor for DbContext.    at
    Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider
    scopedProvider, IDbContextOptions contextOptions, DbContext context)<br>
    at
    Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() 
    at Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker()    at
    Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter[TDeclaringType,TValue](Func`2
    getter, Object target)    at
    Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.BindModelCoreAsync(ModelBindingContext
    bindingContext)    at
    Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext
    actionContext, IModelBinder modelBinder, IValueProvider valueProvider,
    ParameterDescriptor parameter, ModelMetadata metadata, Object value)<br>
    at
    Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.&lt;>c__DisplayClass0_0.&lt;g__Bind|0>d.MoveNext()</p>
  </blockquote>
  
  <p>Exception when I remove the empty constructor from the context</p>
  
  <blockquote>
    <p>System.InvalidOperationException: Could not create an instance of type
    'Skillcheck.Models.TicketsystemContext'. Model bound complex types
    must not be abstract or value types and must have a parameterless
    constructor. Alternatively, give the 'c' parameter a non-null default
    value.    at
    Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext
    bindingContext)    at
    Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.BindModelCoreAsync(ModelBindingContext
    bindingContext)    at
    Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext
    actionContext, IModelBinder modelBinder, IValueProvider valueProvider,
    ParameterDescriptor parameter, ModelMetadata metadata, Object value)<br>
    at
    Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.&lt;>c__DisplayClass0_0.&lt;g__Bind|0>d.MoveNext()</p>
  </blockquote>
  
  <ul>
  <li>cs in the Service isn't null and reads the string from
  appsettings.json successfully</li>
  <li>after services.AddDbContext the context is in the ServiceCollection
  and it doesn't throw</li>
  <li>using the generic DbContextOptions instead of
  DbContextOptions &lt; TicketsystemContext > in the context constructor
  doesn't work</li>
  <li>explicitly adding a IHttpContextAccessor doesn't work either</li>
  </ul>
  
  <p>I temporarily solved it by overriding OnConfiguring, but I want to understand why it's not working.
  I'm using .Net Core 2.1 on VS2017.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>The error was that instead of </p>
  
  <pre><code>private TicketsystemContext _c;
  
  public HomeController(TicketsystemContext c)
  {
      _c = c;
  }
  
  public IActionResult Index()
  {
      return View(_c.User.First());
  }
  </code></pre>
  
  <p>I used </p>
  
  <pre><code>public IActionResult Index(TicketsystemContext c)
  {
      return View(c.User.First());
  }
  </code></pre>
  
  <p>which works when overriding OnConfiguring, but doesn't when configuring while injecting.</p>